Download image from web and set it to the ImageView iOS swift

Solution:

In iOS you can download the image from asynchronously and set it to the imageView.It will not disturb your main thread and UI updates.

You can use the below function as a common and every time you can call the function with its url and imageView.

Code:

    func downloadImage(url: URL,imageView: UIImageView)  {
        print("Download Started")
        getDataFromUrl(url: url) { (data, response, error)  in
            guard let data = data, error == nil else { return }
            print(response?.suggestedFilename ?? url.lastPathComponent)
            print("Download Finished")
            DispatchQueue.main.async() { () -> Void in
                imageView.image = UIImage(data: data)
            }
        }
    }



You can call the function like below

downloadImage(url:yourUrl,imageView:myImageView)

Comments

Popular posts from this blog

Invalid bundle error while upload the app to the app Store

store cgpoint in userdefaults iOS swift