Created
May 11, 2017 09:30
-
-
Save chriswebb09/28dfaf2fb218bf5367fb1e8c21a0b36a 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
| 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