Add image to alert view

后端 未结 5 1794
鱼传尺愫
鱼传尺愫 2021-02-02 12:08

I have an alert view that pops up when the user press the add button. How do i add an image to the alert view?

I added some code that i took reference from stack overfl

5条回答
  •  灰色年华
    2021-02-02 12:35

    We can add image as one option in alert view controller like this.

       let imageView = UIImageView(frame: CGRect(origin: CGPoint(x: 0,y :0), size: CGSize(width: 196, height: 196)))
        imageView.image = image
    
        UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, imageView.isOpaque, 0.0)
        defer { UIGraphicsEndImageContext() }
        let context = UIGraphicsGetCurrentContext()
        imageView.layer.render(in: context!)
        let finalImage = UIGraphicsGetImageFromCurrentImageContext()
    
        let alertMessage = UIAlertController(title: "Your Title", message: "", preferredStyle: .alert)
        let action = UIAlertAction(title: "", style: .default, handler: nil)
        action.setValue(finalImage?.withRenderingMode(UIImageRenderingMode.alwaysOriginal), forKey: "image")
        alertMessage .addAction(action)
        let action1 = UIAlertAction(title: "OK", style: .default, handler: nil)
        alertMessage .addAction(action1)
    
        self.present(alertMessage, animated: true, completion: nil)
    

提交回复
热议问题