将图像添加到警报视图

Add image to alert view

我有一个警告视图,当用户按下添加按钮时弹出。如何将图像添加到警报视图?

我添加了一些从堆栈溢出中引用的代码。我的保存按钮被替换为图像并且图像显示为蓝色...

警报视图代码

var alert = UIAlertController(title: "Spring Element \(springNumber)",
            message: "Add spring properties",
            preferredStyle: .Alert)

        let saveAction = UIAlertAction(title: "Save",
            style: .Default) { (action: UIAlertAction!) -> Void in

                let textField1 = alert.textFields![0] as UITextField
                self.txtField1.append(textField1.text)
                self.tableView.reloadData()

                let textField2 = alert.textFields![1] as UITextField
                self.txtField2.append(textField2.text)
                self.tableView.reloadData()

                println(self.txtField1)
                println(self.txtField2)
        }

        let cancelAction = UIAlertAction(title: "Cancel",
            style: .Default) { (action: UIAlertAction!) -> Void in
        }

        //adding textfield1
        alert.addTextFieldWithConfigurationHandler {
            (textField1: UITextField!) -> Void in
            textField1.placeholder = "Force"
        }

        //adding textfield2
        alert.addTextFieldWithConfigurationHandler {
            (textField2: UITextField!) -> Void in
            textField2.placeholder = "Stiffness"
        }

        alert.addAction(saveAction)
        alert.addAction(cancelAction)

        presentViewController(alert,
            animated: true,
            completion: nil)

图片查看代码

   let image = UIImage(named: "springAtWall")
    saveAction.setValue(image, forKey: "image")
    alert.addAction(saveAction)

是的,您可以添加一个 UIImageView 作为您的警报视图的子视图。

var imageView = UIImageView(frame: CGRect(x: 220, y: 10, width: 40, height: 40))
imageView.image = yourImage

alert.view.addSubview(imageView)

Swift 4:

var imageView = UIImageView(frame: CGRect(x: 220, y: 10, width: 40, height: 40))
imageView.image = <#yourImage#>
alert.view.addSubview(imageView)

我们可以像这样在警报视图控制器中添加图像作为一个选项。

   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)

这是 Swift 4 的解决方案:

let showAlert = UIAlertController(title: "Demo Alert", message: nil, preferredStyle: .alert)
let imageView = UIImageView(frame: CGRect(x: 10, y: 50, width: 250, height: 230))
imageView.image = image // Your image here...
showAlert.view.addSubview(imageView)
let height = NSLayoutConstraint(item: showAlert.view, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 320)
let width = NSLayoutConstraint(item: showAlert.view, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 250)
showAlert.view.addConstraint(height)
showAlert.view.addConstraint(width)
showAlert.addAction(UIAlertAction(title: "OK", style: .default, handler: { action in
    // your actions here...    
}))
self.present(showAlert, animated: true, completion: nil)

所有 iPhone 的输出都将如下所示:

我不是很清楚@Kakumanu 的 ImageContext 的用途,但那是为了调整 UIImage 的大小。也许更快速的方法是通过 UIImage 扩展 ```

extension UIImage {
/// Resize a UIImage
func imageWith(newSize: CGSize) -> UIImage {
    let renderer = UIGraphicsImageRenderer(size: newSize)
    let image = renderer.image { _ in
        self.draw(in: CGRect.init(origin: CGPoint.zero, size: newSize))
    }
    return image.withRenderingMode(self.renderingMode)
 }
}
func launchAlertTool(image: UIImage, topViewController: UIViewController) {
        let alert = UIAlertController(title: "Add Comments",
                                      message: nil,
                                      preferredStyle: .alert)
        
        let subview = (alert.view.subviews.first?.subviews.first?.subviews.first!)! as UIView
        subview.backgroundColor = UIColor(red: (145/255.0), green: (200/255.0), blue: (0/255.0), alpha: 1.0)
        let imageView = UIImageView(frame: CGRect(x: 0, y: 100, width: topViewController.view.bounds.width, height: topViewController.view.bounds.width - 150))
        imageView.image = image
        imageView.contentMode = .scaleAspectFit
        alert.view.addSubview(imageView)
        alert.view.tintColor = .black
        
        let height = NSLayoutConstraint(item: alert.view as Any, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: topViewController.view.bounds.width)
        let width = NSLayoutConstraint(item: alert.view as Any, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: topViewController.view.bounds.width)
        alert.view.addConstraint(height)
        alert.view.addConstraint(width)
        
        alert.addTextField { (textField) in
            // optionally configure the text field
            textField.keyboardType = .alphabet
            textField.textAlignment = .center
            textField.tintColor = .systemPink
        }
        
        let okAction = UIAlertAction(title: "Share", style: .default) { [unowned alert] (action) in
            if let textField = alert.textFields?.first {
                let comment = textField.text ?? "Check this image"
                let shareAll = [comment, screenShot] as [Any]
                let activityViewController = UIActivityViewController(activityItems: shareAll, applicationActivities: nil)
                activityViewController.popoverPresentationController?.sourceView = topViewController.view
                topViewController.navigationController?.present(activityViewController, animated: true, completion: {
                })
            }
        }
        alert.addAction(okAction)
        if let topVC = UIApplication.getTopViewController() {
            topVC.present(alert, animated: true, completion: {
            })
        }
    }