Skip to content

Instantly share code, notes, and snippets.

@alexpaul
Created April 25, 2018 13:36
Show Gist options
  • Save alexpaul/d4e22505c6de182ae949a4f948ebc8e8 to your computer and use it in GitHub Desktop.
Save alexpaul/d4e22505c6de182ae949a4f948ebc8e8 to your computer and use it in GitHub Desktop.
//
// ImageCache.swift
// CatOrDog
//
// Created by Alex Paul on 12/19/17.
// Copyright © 2017 Alex Paul. All rights reserved.
//
// This is a singleton to manage our NSCache objects
// NSCache saves temporary files in the caches directory ]
import UIKit
class ImageCache {
private init(){}
static let manager = ImageCache()
private let sharedCached = NSCache<NSString, UIImage>()
// get current cached image
func cachedImage(url: URL) -> UIImage? {
return sharedCached.object(forKey: url.absoluteString as NSString)
}
// process image and store in cache
func processImageInBackground(imageURL: URL, completion: @escaping(Error?, UIImage?) -> Void) {
DispatchQueue.global().async {
do {
let imageData = try Data.init(contentsOf: imageURL)
let image = UIImage.init(data: imageData)
// store image in cache
if let image = image {
self.sharedCached.setObject(image, forKey: imageURL.absoluteString as NSString)
}
completion(nil, image)
} catch {
print("image processing error: \(error.localizedDescription)")
completion(error, nil)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment