@@ -79,7 +79,7 @@ public extension UIImageView {
79
79
return
80
80
}
81
81
82
- if let cachedImage = Downloader . cache . object ( forKey: url as AnyObject ) as? UIImage {
82
+ if let cachedImage = ImageCache . shared . getImage ( forKey: url. absoluteString ) {
83
83
handleSuccess ( cachedImage, url)
84
84
return
85
85
}
@@ -98,7 +98,7 @@ public extension UIImageView {
98
98
99
99
DispatchQueue . main. async {
100
100
if response? . url == url {
101
- Downloader . cache . setObject ( image, forKey: url as AnyObject )
101
+ ImageCache . shared . setImage ( image, forKey: url. absoluteString )
102
102
handleSuccess ( image, url)
103
103
} else {
104
104
failure ? ( ImageDownloadError . urlMismatch)
@@ -117,7 +117,7 @@ public extension UIImageView {
117
117
/// and we need to prevent returning the (old) cached entry.
118
118
///
119
119
@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 )
121
121
122
122
// Remove all cached responses - removing an individual response does not work since iOS 7.
123
123
// This feels hacky to do but what else can we do...
@@ -156,15 +156,9 @@ public extension UIImageView {
156
156
}
157
157
}
158
158
159
-
160
159
/// Private helper structure
161
160
///
162
161
private struct Downloader {
163
-
164
- /// Stores all of the previously downloaded images.
165
- ///
166
- static let cache = NSCache < AnyObject , AnyObject > ( )
167
-
168
162
/// Key used to associate the current URL.
169
163
///
170
164
static var urlKey = " urlKey "
@@ -174,3 +168,23 @@ public extension UIImageView {
174
168
static var taskKey = " downloadTaskKey "
175
169
}
176
170
}
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