Downloading image from a URL on Swift


Sun Jan 12 2020 17:30:26 GMT+0000 (Coordinated Universal Time)

Saved by @19sunflower91 #ios #swift #swift4

let url = URL(string: image.url)
let data = try? Data(contentsOf: url!) //make sure your image in this url does exist, otherwise unwrap in a if let check / try-catch
imageView.image = UIImage(data: data!)

It's important to note that the Data(contentsOf:) method will download the contents of the url synchronously in the same thread the code is being executed, so do not invoke this in the main thread of your application.