@@ -79,7 +79,7 @@ public extension UIImageView {
7979 return
8080 }
8181
82- if let cachedImage = Downloader . cache . object ( forKey: url as AnyObject ) as? UIImage {
82+ if let cachedImage = ImageCache . shared . getImage ( forKey: url. absoluteString ) {
8383 handleSuccess ( cachedImage, url)
8484 return
8585 }
@@ -98,7 +98,7 @@ public extension UIImageView {
9898
9999 DispatchQueue . main. async {
100100 if response? . url == url {
101- Downloader . cache . setObject ( image, forKey: url as AnyObject )
101+ ImageCache . shared . setImage ( image, forKey: url. absoluteString )
102102 handleSuccess ( image, url)
103103 } else {
104104 failure ? ( ImageDownloadError . urlMismatch)
@@ -117,7 +117,7 @@ public extension UIImageView {
117117 /// and we need to prevent returning the (old) cached entry.
118118 ///
119119 @objc func overrideImageCache( for url: URL , with image: UIImage ) {
120- Downloader . cache . setObject ( image, forKey: url as AnyObject )
120+ ImageCache . shared . setImage ( image, forKey: url. absoluteString )
121121
122122 // Remove all cached responses - removing an individual response does not work since iOS 7.
123123 // This feels hacky to do but what else can we do...
@@ -156,15 +156,9 @@ public extension UIImageView {
156156 }
157157 }
158158
159-
160159 /// Private helper structure
161160 ///
162161 private struct Downloader {
163-
164- /// Stores all of the previously downloaded images.
165- ///
166- static let cache = NSCache < AnyObject , AnyObject > ( )
167-
168162 /// Key used to associate the current URL.
169163 ///
170164 static var urlKey = " urlKey "
@@ -174,3 +168,23 @@ public extension UIImageView {
174168 static var taskKey = " downloadTaskKey "
175169 }
176170}
171+
172+ public protocol ImageCaching {
173+ func setImage( _ image: UIImage , forKey key: String )
174+ func getImage( forKey key: String ) -> UIImage ?
175+ }
176+
177+ public class ImageCache : ImageCaching {
178+ private let cache = NSCache < NSString , UIImage > ( )
179+
180+ /// Changes the default cache used by the image dowloader.
181+ public static var shared : ImageCaching = ImageCache ( )
182+
183+ public func setImage( _ image: UIImage , forKey key: String ) {
184+ cache. setObject ( image, forKey: key as NSString )
185+ }
186+
187+ public func getImage( forKey key: String ) -> UIImage ? {
188+ cache. object ( forKey: key as NSString )
189+ }
190+ }
0 commit comments