从Web URL在UIAlertAction中添加图像

时间:2019-02-14 06:25:33

标签: ios swift uialertcontroller uiactionsheet sdwebimage

enter image description here

有什么办法可以在我已经显示的UIAlertController动作中显示来自Web URL的图像。真正的场景是我展示了UIAlertController,并且我从URL并排获取图像。但是现在我想在下载图像后更新UIAlertAction,但是它们没有更新。

稍后,如果我打开UIAlertController,则这些图像将显示在UIAlertAction中。

let alertController = UIAlertController(title: "My App", message: "Select option:", preferredStyle: .actionSheet)

        for url in arrUrl {
            let action = UIAlertAction(title: "url", style: .default) { (action) in

            }

            downloadImage(url) { (image) in
                if image != nil {
                    action.setValue(image, forKey: "image")
                }
            }
            alertController.addAction(action)
        }

func downloadImage(_ strUrl: String, completionHandler: @escaping(_ image: UIImage?) -> ()) {

        SDWebImageManager.shared().loadImage(
            with: URL(string: strUrl),
            options: .highPriority,
            progress: nil) { (image, data, error, cacheType, isFinished, imageUrl) in
                completionHandler(image)

        }
    }

1 个答案:

答案 0 :(得分:0)

是的,可以这样做。您可以扩展您引用的博客文章(https://medium.com/@maximbilan/ios-uialertcontroller-customization-5cfd88140db8)上使用的模式。

可以使用UISwitch来代替UIImageView。您可以将保存UIViewController UIImageView的{​​{1}}保存在当前的视图控制器中。

获取图像后,您可以更新IBOutlet的{​​{1}}属性。

免责声明:

我很确定此image业务是不受支持的黑客活动,因为UIImageView属性似乎没有记录在案。因此,尽管这可能在今天起作用,但它随时可能中断。提交到AppStore时也可能会被拒绝。

使用后果自负