I\'d like to load an image from a URL in my application, so I first tried with Objective-C and it worked, however, with Swift, I\'ve a compilation error:
Xcode 8 • Swift 3
Leo Dabus's answer is awesome! I just wanted to provide an all-in-one function solution:
let url = URL(string:
"http://www.apple.com/euro/ios/ios8/a/generic/images/og.png")
let task = URLSession.shared.dataTask(with: url!) { data, response, error in
guard let data = data, error == nil else { return }
DispatchQueue.main.async() { // execute on main thread
self.imageView.image = UIImage(data: data)
}
}
task.resume()