Skip to content
This repository was archived by the owner on Jun 4, 2025. It is now read-only.

Commit 8b7b951

Browse files
authored
Merge pull request #129 from wordpress-mobile/task/make-cache-customizable
Make image cache customizable
2 parents b9476a2 + 480591a commit 8b7b951

File tree

3 files changed

+26
-24
lines changed

3 files changed

+26
-24
lines changed

CHANGELOG.md

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,11 @@ _None._
3030
3131
-->
3232

33-
## Unreleased
34-
35-
### Breaking Changes
36-
37-
_None._
33+
## 1.14.0
3834

3935
### New Features
4036

41-
_None._
42-
43-
### Bug Fixes
44-
45-
_None._
46-
47-
### Internal Changes
48-
49-
_None._
37+
- Add a new `ImageCaching` protocol and an `ImageCache` class with a `shared` property to allow overriding the default image cache [#129]
5038

5139
## 1.13.1
5240

Sources/WordPressUI/Extensions/UIImageView+Networking.swift

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}

WordPressUI.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Pod::Spec.new do |s|
44
s.name = 'WordPressUI'
5-
s.version = '1.13.1'
5+
s.version = '1.14.0'
66

77
s.summary = 'Home of reusable WordPress UI components.'
88
s.description = <<-DESC

0 commit comments

Comments
 (0)