Skip to content

Instantly share code, notes, and snippets.

@sgl0v
Created November 3, 2019 18:16
Show Gist options
  • Save sgl0v/3f5cf1ff76d1cf00b859c0da315d9967 to your computer and use it in GitHub Desktop.
Save sgl0v/3f5cf1ff76d1cf00b859c0da315d9967 to your computer and use it in GitHub Desktop.
extension ImageCache: ImageCacheType {
func insertImage(_ image: UIImage?, for url: URL) {
guard let image = image else { return removeImage(for: url) }
let decodedImage = image.decodedImage()
lock.lock(); defer { lock.unlock() }
imageCache.setObject(decodedImage, forKey: url as AnyObject)
decodedImageCache.setObject(image as AnyObject, forKey: url as AnyObject, cost: decodedImage.diskSize)
}
func removeImage(for url: URL) {
lock.lock(); defer { lock.unlock() }
imageCache.removeObject(forKey: url as AnyObject)
decodedImageCache.removeObject(forKey: url as AnyObject)
}
}
@sawarren
Copy link

Hello, I was reading Reusable Image Cache in Swift
This code is adding the decoded image to the encoded cache and vise versa.

Also, you don't say what 'diskSize' is: presumably another convenience extension?

Other than that LGTM 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment