Skip to content

Instantly share code, notes, and snippets.

@chriseidhof
Created July 11, 2019 11:48
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chriseidhof/8ed01fc1169304603f9ac01e2fd50c7a to your computer and use it in GitHub Desktop.
Save chriseidhof/8ed01fc1169304603f9ac01e2fd50c7a to your computer and use it in GitHub Desktop.
Image Manager
final fileprivate class Weak<A: AnyObject> {
weak var value: A?
init(_ value: A) {
self.value = value
}
}
final class ImageManager {
fileprivate var values: [URL:Weak<Resource<UIImage>>] = [:]
func load(url: URL) -> Resource<UIImage> {
if let v = values[url]?.value { return v }
let loader = Resource(Endpoint(imageURL: url))
values[url] = Weak(loader)
return loader
}
static let shared = ImageManager()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment