Created
May 11, 2017 15:20
-
-
Save chriswebb09/5021b65f9c0d6f6158460df2c9f15fc5 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
import UIKit | |
fileprivate let imageCache = NSCache<NSString, UIImage>() | |
extension UIImageView { | |
func downloadImage(url: URL) { | |
if let cachedImage = imageCache.object(forKey: url.absoluteString as NSString) { | |
self.image = cachedImage | |
return | |
} | |
URLSession(configuration: .ephemeral).dataTask(with: URLRequest(url: url)) { data, response, error in | |
if error != nil { | |
print(error?.localizedDescription ?? "Unknown error") | |
} | |
DispatchQueue.main.async { | |
if let data = data, let image = UIImage(data: data) { | |
imageCache.setObject(image, forKey: url.absoluteString as NSString) | |
self.image = image | |
} | |
} | |
}.resume() | |
} | |
func setRounded(frame: CGRect) { | |
let radius = frame.height / 2 | |
layer.cornerRadius = radius | |
layer.masksToBounds = true | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment