Skip to content

Commit 58e04d5

Browse files
committed
Merge pull request #95 from onevcat/feature/resource-based-api
Resource based api
2 parents 7ffb65c + bc6f480 commit 58e04d5

File tree

7 files changed

+462
-81
lines changed

7 files changed

+462
-81
lines changed

Kingfisher-Demo/ViewController.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ extension ViewController: UICollectionViewDataSource {
5858
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
5959
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("collectionViewCell", forIndexPath: indexPath) as! CollectionViewCell
6060
cell.cellImageView.kf_showIndicatorWhenLoading = true
61-
cell.cellImageView.kf_setImageWithURL(NSURL(string: "https://raw.githubusercontent.com/onevcat/Kingfisher/master/images/kingfisher-\(indexPath.row + 1).jpg")!, placeholderImage: nil, optionsInfo: nil, progressBlock: { (receivedSize, totalSize) -> () in
61+
62+
let URL = NSURL(string: "https://raw.githubusercontent.com/onevcat/Kingfisher/master/images/kingfisher-\(indexPath.row + 1).jpg")!
63+
cell.cellImageView.kf_setImageWithResource(Resource(downloadURL: URL), placeholderImage: nil, optionsInfo: nil, progressBlock: { (receivedSize, totalSize) -> () in
6264
println("\(indexPath.row + 1): \(receivedSize)/\(totalSize)")
6365
}) { (image, error, cacheType, imageURL) -> () in
6466
println("\(indexPath.row + 1): Finished")

Kingfisher.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
/* Begin PBXBuildFile section */
1010
0D9C68098E20AB4F19D7C313 /* libPods-KingfisherTests.a in Frameworks */ = {isa = PBXBuildFile; fileRef = A9E621E297FEFAD35D39C34E /* libPods-KingfisherTests.a */; };
11+
4B2C4DF81B7D7ACD000912CA /* Resource.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4B2C4DF71B7D7ACD000912CA /* Resource.swift */; };
1112
4B3E714F1B02005900F5AAED /* WatchKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 4B3E714D1B01FEB200F5AAED /* WatchKit.framework */; settings = {ATTRIBUTES = (Weak, ); }; };
1213
4B412CA51AE8A2F9008D530A /* KingfisherOptionsInfo.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4B412CA41AE8A2F9008D530A /* KingfisherOptionsInfo.swift */; };
1314
4B6D4F651AE0A46D0084D15B /* UIImageViewExtensionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4B6D4F641AE0A46D0084D15B /* UIImageViewExtensionTests.swift */; };
@@ -107,6 +108,7 @@
107108
/* End PBXCopyFilesBuildPhase section */
108109

109110
/* Begin PBXFileReference section */
111+
4B2C4DF71B7D7ACD000912CA /* Resource.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Resource.swift; sourceTree = "<group>"; };
110112
4B3E714D1B01FEB200F5AAED /* WatchKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = WatchKit.framework; path = System/Library/Frameworks/WatchKit.framework; sourceTree = SDKROOT; };
111113
4B412CA41AE8A2F9008D530A /* KingfisherOptionsInfo.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = KingfisherOptionsInfo.swift; sourceTree = "<group>"; };
112114
4B6D4F641AE0A46D0084D15B /* UIImageViewExtensionTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIImageViewExtensionTests.swift; sourceTree = "<group>"; };
@@ -295,6 +297,7 @@
295297
D1ED2D591AD2D0F900CFC3EB /* KingfisherManager.swift */,
296298
D1ED2D5A1AD2D0F900CFC3EB /* KingfisherOptions.swift */,
297299
4B412CA41AE8A2F9008D530A /* KingfisherOptionsInfo.swift */,
300+
4B2C4DF71B7D7ACD000912CA /* Resource.swift */,
298301
D1ED2D371AD2D09F00CFC3EB /* Supporting Files */,
299302
);
300303
path = Kingfisher;
@@ -605,6 +608,7 @@
605608
4BD352F31AF36A0700B18A40 /* WKInterfaceImage+Kingfisher.swift in Sources */,
606609
D1ED2D5E1AD2D0F900CFC3EB /* ImageDownloader.swift in Sources */,
607610
4B412CA51AE8A2F9008D530A /* KingfisherOptionsInfo.swift in Sources */,
611+
4B2C4DF81B7D7ACD000912CA /* Resource.swift in Sources */,
608612
D1ED2D601AD2D0F900CFC3EB /* KingfisherOptions.swift in Sources */,
609613
D151E72B1AD3C48D004FD4AE /* UIImage+Decode.swift in Sources */,
610614
);

Kingfisher/KingfisherManager.swift

Lines changed: 61 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -95,24 +95,24 @@ public class KingfisherManager {
9595
cache = ImageCache.defaultCache
9696
downloader = ImageDownloader.defaultDownloader
9797
}
98-
98+
9999
/**
100-
Get an image with URL as the key.
100+
Get an image with resource.
101101
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`.
103103
These default behaviors could be adjusted by passing different options. See `KingfisherOptions` for more.
104104

105-
:param: URL The image URL.
105+
:param: resource Resource object contains information such as `cacheKey` and `downloadURL`.
106106
:param: optionsInfo A dictionary could control some behaviors. See `KingfisherOptionsInfo` for more.
107107
:param: progressBlock Called every time downloaded data changed. This could be used as a progress UI.
108108
:param: completionHandler Called when the whole retriving process finished.
109109

110110
:returns: A `RetrieveImageTask` task object. You can use this object to cancel the task.
111111
*/
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
116116
{
117117
func parseOptionsInfo(optionsInfo: KingfisherOptionsInfo?) -> (Options, ImageCache, ImageDownloader) {
118118
let options: Options
@@ -142,42 +142,64 @@ public class KingfisherManager {
142142
let parsedOptions = parseOptionsInfo(optionsInfo)
143143
let (options, targetCache, downloader) = (parsedOptions.0, parsedOptions.1, parsedOptions.2)
144144

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)
176159
}
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
177175
}
178176

179177
return task
180178
}
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+
}
181203

182204
func downloadAndCacheImageWithURL(URL: NSURL,
183205
forKey key: String,

Kingfisher/Resource.swift

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//
2+
// Resource.swift
3+
// Kingfisher
4+
//
5+
// Created by Wei Wang on 15/4/6.
6+
//
7+
// Copyright (c) 2015 Wei Wang <[email protected]>
8+
//
9+
// Permission is hereby granted, free of charge, to any person obtaining a copy
10+
// of this software and associated documentation files (the "Software"), to deal
11+
// in the Software without restriction, including without limitation the rights
12+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
// copies of the Software, and to permit persons to whom the Software is
14+
// furnished to do so, subject to the following conditions:
15+
//
16+
// The above copyright notice and this permission notice shall be included in
17+
// all copies or substantial portions of the Software.
18+
//
19+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
// THE SOFTWARE.
26+
27+
import Foundation
28+
29+
public struct Resource {
30+
public let cacheKey: String
31+
public let downloadURL: NSURL
32+
33+
public init(downloadURL: NSURL, cacheKey: String? = nil) {
34+
self.downloadURL = downloadURL
35+
self.cacheKey = cacheKey ?? downloadURL.absoluteString!
36+
}
37+
}

0 commit comments

Comments
 (0)