从web url在UIAlertAction中添加图像

问题描述 投票:0回答:1

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)

        }
    }
ios swift uialertcontroller uiactionsheet sdwebimage
1个回答
0
投票

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

你可以使用UISwitch而不是使用UIImageView。你可以保存在你的呈现视图控制器中保存UIViewController UIImageViewIBOutlet

获取图像后,您可以更新imageUIImageView属性。

免责声明:

我很确定这个alertAction.setValue(..., forKey: "contentViewController")业务是一个不受支持的黑客,因为contentViewController属性似乎没有记录。所以,虽然这可能在今天起作用,但它可能在任何时候都会破裂。它也可能在提交给AppStore时被拒绝。

使用风险由您自己承担

© www.soinside.com 2019 - 2024. All rights reserved.