@@ -95,24 +95,24 @@ public class KingfisherManager {
95
95
cache = ImageCache . defaultCache
96
96
downloader = ImageDownloader . defaultDownloader
97
97
}
98
-
98
+
99
99
/**
100
- Get an image with URL as the key .
100
+ Get an image with resource .
101
101
If KingfisherOptions.None is used as `options`, Kingfisher will seek the image in memory and disk first.
102
- If not found, it will download the image at URL and cache it.
102
+ If not found, it will download the image at `resource.downloadURL` and cache it with `resource.cacheKey` .
103
103
These default behaviors could be adjusted by passing different options. See `KingfisherOptions` for more.
104
104
105
- :param: URL The image URL .
105
+ :param: resource Resource object contains information such as `cacheKey` and `downloadURL` .
106
106
:param: optionsInfo A dictionary could control some behaviors. See `KingfisherOptionsInfo` for more.
107
107
:param: progressBlock Called every time downloaded data changed. This could be used as a progress UI.
108
108
:param: completionHandler Called when the whole retriving process finished.
109
109
110
110
:returns: A `RetrieveImageTask` task object. You can use this object to cancel the task.
111
111
*/
112
- public func retrieveImageWithURL ( URL : NSURL ,
113
- optionsInfo: KingfisherOptionsInfo ? ,
114
- progressBlock: DownloadProgressBlock ? ,
115
- completionHandler: CompletionHandler ? ) -> RetrieveImageTask
112
+ public func retrieveImageWithResource ( resource : Resource ,
113
+ optionsInfo: KingfisherOptionsInfo ? ,
114
+ progressBlock: DownloadProgressBlock ? ,
115
+ completionHandler: CompletionHandler ? ) -> RetrieveImageTask
116
116
{
117
117
func parseOptionsInfo( optionsInfo: KingfisherOptionsInfo ? ) -> ( Options , ImageCache , ImageDownloader ) {
118
118
let options : Options
@@ -142,42 +142,64 @@ public class KingfisherManager {
142
142
let parsedOptions = parseOptionsInfo ( optionsInfo)
143
143
let ( options, targetCache, downloader) = ( parsedOptions. 0 , parsedOptions. 1 , parsedOptions. 2 )
144
144
145
- if let key = URL . absoluteString {
146
- if options. forceRefresh {
147
- downloadAndCacheImageWithURL ( URL,
148
- forKey: key,
149
- retrieveImageTask: task,
150
- progressBlock: progressBlock,
151
- completionHandler: completionHandler,
152
- options: options,
153
- targetCache: targetCache,
154
- downloader: downloader)
155
- } else {
156
- let diskTaskCompletionHandler : CompletionHandler = { ( image, error, cacheType, imageURL) -> ( ) in
157
- // Break retain cycle created inside diskTask closure below
158
- task. diskRetrieveTask = nil
159
- completionHandler ? ( image: image, error: error, cacheType: cacheType, imageURL: imageURL)
160
- }
161
- let diskTask = targetCache. retrieveImageForKey ( key, options: options, completionHandler: { ( image, cacheType) -> ( ) in
162
- if image != nil {
163
- diskTaskCompletionHandler ( image: image, error: nil , cacheType: cacheType, imageURL: URL)
164
- } else {
165
- self . downloadAndCacheImageWithURL ( URL,
166
- forKey: key,
167
- retrieveImageTask: task,
168
- progressBlock: progressBlock,
169
- completionHandler: diskTaskCompletionHandler,
170
- options: options,
171
- targetCache: targetCache,
172
- downloader: downloader)
173
- }
174
- } )
175
- task. diskRetrieveTask = diskTask
145
+ if options. forceRefresh {
146
+ downloadAndCacheImageWithURL ( resource. downloadURL,
147
+ forKey: resource. cacheKey,
148
+ retrieveImageTask: task,
149
+ progressBlock: progressBlock,
150
+ completionHandler: completionHandler,
151
+ options: options,
152
+ targetCache: targetCache,
153
+ downloader: downloader)
154
+ } else {
155
+ let diskTaskCompletionHandler : CompletionHandler = { ( image, error, cacheType, imageURL) -> ( ) in
156
+ // Break retain cycle created inside diskTask closure below
157
+ task. diskRetrieveTask = nil
158
+ completionHandler ? ( image: image, error: error, cacheType: cacheType, imageURL: imageURL)
176
159
}
160
+ let diskTask = targetCache. retrieveImageForKey ( resource. cacheKey, options: options, completionHandler: { ( image, cacheType) -> ( ) in
161
+ if image != nil {
162
+ diskTaskCompletionHandler ( image: image, error: nil , cacheType: cacheType, imageURL: resource. downloadURL)
163
+ } else {
164
+ self . downloadAndCacheImageWithURL ( resource. downloadURL,
165
+ forKey: resource. cacheKey,
166
+ retrieveImageTask: task,
167
+ progressBlock: progressBlock,
168
+ completionHandler: diskTaskCompletionHandler,
169
+ options: options,
170
+ targetCache: targetCache,
171
+ downloader: downloader)
172
+ }
173
+ } )
174
+ task. diskRetrieveTask = diskTask
177
175
}
178
176
179
177
return task
180
178
}
179
+
180
+ /**
181
+ Get an image with `URL.absoluteString` as the key.
182
+ If KingfisherOptions.None is used as `options`, Kingfisher will seek the image in memory and disk first.
183
+ If not found, it will download the image at URL and cache it with `URL.absoluteString` value as its key.
184
+
185
+ If you need to specify the key other than `URL.absoluteString`, please use resource version of this API with `resource.cacheKey` set to what you want.
186
+
187
+ These default behaviors could be adjusted by passing different options. See `KingfisherOptions` for more.
188
+
189
+ :param: URL The image URL.
190
+ :param: optionsInfo A dictionary could control some behaviors. See `KingfisherOptionsInfo` for more.
191
+ :param: progressBlock Called every time downloaded data changed. This could be used as a progress UI.
192
+ :param: completionHandler Called when the whole retriving process finished.
193
+
194
+ :returns: A `RetrieveImageTask` task object. You can use this object to cancel the task.
195
+ */
196
+ public func retrieveImageWithURL( URL: NSURL ,
197
+ optionsInfo: KingfisherOptionsInfo ? ,
198
+ progressBlock: DownloadProgressBlock ? ,
199
+ completionHandler: CompletionHandler ? ) -> RetrieveImageTask
200
+ {
201
+ return retrieveImageWithResource ( Resource ( downloadURL: URL) , optionsInfo: optionsInfo, progressBlock: progressBlock, completionHandler: completionHandler)
202
+ }
181
203
182
204
func downloadAndCacheImageWithURL( URL: NSURL ,
183
205
forKey key: String ,
0 commit comments