Skip to content

Instantly share code, notes, and snippets.

@chriswebb09
Created May 11, 2017 09:30
Show Gist options
  • Select an option

  • Save chriswebb09/28dfaf2fb218bf5367fb1e8c21a0b36a to your computer and use it in GitHub Desktop.

Select an option

Save chriswebb09/28dfaf2fb218bf5367fb1e8c21a0b36a to your computer and use it in GitHub Desktop.
static func downloadImage(url: URL, completion: @escaping (_ image: UIImage?, _ error: Error? ) -> Void) {
if let cachedImage = imageCache.object(forKey: url.absoluteString as NSString) {
completion(cachedImage, nil)
} else {
MTAPIClient.downloadData(url: url) { data, response, error in
if let error = error {
completion(nil, error)
} else if let data = data, let image = UIImage(data: data) {
imageCache.setObject(image, forKey: url.absoluteString as NSString)
completion(image, nil)
} else {
completion(nil, NSError.generalParsingError(domain: url.absoluteString))
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment