iOS – Image Downloader helper class (Swift 3)

Image Downloader Class written in Swift 3 to download images.

class Downloader {

        class func downloadImageWithURL(_ url:String) -> UIImage? {

           let data = try? Data(contentsOf: URL(string: url)!)

         if let imgData = data {

            return UIImage(data: imgData)

        }

        else {

            print(“\n\nThis image url may be wrong : \n \(url)”)

            return nil

        }

    }

} // class end

You can then use this class to download and show image in your UIImage  as below:

        let image  =  Downloader.downloadImageWithURL(“http://images.freeimages.com/images/premium/small-comps/3352/33528960-palouse-waterfall-washington.jpg”)

yourImageView.image = image

Then you are done. Enjoy… 🙂