Skip to content

Instantly share code, notes, and snippets.

@chriswebb09
Created May 11, 2017 15:20
Show Gist options
  • Save chriswebb09/5021b65f9c0d6f6158460df2c9f15fc5 to your computer and use it in GitHub Desktop.
Save chriswebb09/5021b65f9c0d6f6158460df2c9f15fc5 to your computer and use it in GitHub Desktop.
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