0
votes

enter image description here

Is there any way I can show an image from web url in my already shown UIAlertController's action. The real scenario is I've presented the UIAlertController and side by side I'm fetching the images from url. But now I wanted to update the UIAlertAction after image's are downloaded, but they aren't updating.

Later If i open the UIAlertController than those images get displayed in 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
Where did you get the source code from? There is no image property on the UIAlertAction object. - Sachin Vas
@SachinVas There is. We can add image in UIAlertController's action. See this link. medium.com/@maximbilan/… - Rohitax Rajguru
Looks like you are missing .withRenderingMode(UIImage.RenderingMode.alwaysOriginal). - Sachin Vas
@SachinVas No that's not the case. - Rohitax Rajguru
@RohitaxRajguru Perhaps it would be better if you create you'r own custom UIView with buttons/ImageViews, because using "setValue" function is a hack, so proper way is create your own. - Ildar.Z

1 Answers

0
votes

Yes, this can be done. You can extend the pattern used at the blog post you referenced (https://medium.com/@maximbilan/ios-uialertcontroller-customization-5cfd88140db8).

Instead of using a UISwitch, you can use a UIImageView. You can save the UIViewController that holds the UIImageView IBOutlet in your presenting view controller.

After fetching an image, you can update the image property of the UIImageView.

Disclaimer:

I'm pretty sure this alertAction.setValue(..., forKey: "contentViewController") business is an unsupported hack as the contentViewController property does not appear to be documented. So, although this may work today, it could break at any time. It could also get rejected when submitted to the AppStore.

Use at your own risk