Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save chriswebb09/9f99342dd26e9c266ffbf12efa2908a4 to your computer and use it in GitHub Desktop.
Save chriswebb09/9f99342dd26e9c266ffbf12efa2908a4 to your computer and use it in GitHub Desktop.
extension ImageDownloadProtocol {
func downloadImage(from url: URL, completion: @escaping (UIImage?) -> Void) {
let session = URLSession(configuration: .default)
DispatchQueue.global(qos: .background).async {
print("In background")
session.dataTask(with: URLRequest(url: url)) { data, response, error in
if error != nil {
print(error?.localizedDescription ?? "Unknown error")
}
if let data = data, let image = UIImage(data: data) {
print("Downloaded image")
DispatchQueue.main.async {
print("dispatched to main")
completion(image)
}
}
}.resume()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment