Created
May 11, 2017 11:26
-
-
Save chriswebb09/9f99342dd26e9c266ffbf12efa2908a4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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