From aa6050f5e4c991805e291ba41bd2bb9ba1ccb0de Mon Sep 17 00:00:00 2001 From: onevcat Date: Wed, 30 Dec 2015 23:59:48 +0900 Subject: [PATCH] Public task properties --- Kingfisher/ImageDownloader.swift | 7 ++++++- Kingfisher/KingfisherManager.swift | 4 ++-- README.md | 24 ++++++++++++++++++------ 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/Kingfisher/ImageDownloader.swift b/Kingfisher/ImageDownloader.swift index d1c72d5f0..8e8d2a7f9 100644 --- a/Kingfisher/ImageDownloader.swift +++ b/Kingfisher/ImageDownloader.swift @@ -35,11 +35,16 @@ public typealias ImageDownloaderCompletionHandler = ((image: UIImage?, error: NS /// Download task. public struct RetrieveImageDownloadTask { let internalTask: NSURLSessionDataTask - weak var ownerDownloader: ImageDownloader? + + public private(set) weak var ownerDownloader: ImageDownloader? public func cancel() { ownerDownloader?.cancelDownloadingTask(self) } + + public var URL: NSURL? { + return internalTask.originalRequest?.URL + } } private let defaultDownloaderName = "default" diff --git a/Kingfisher/KingfisherManager.swift b/Kingfisher/KingfisherManager.swift index ab9eca502..b854b677e 100644 --- a/Kingfisher/KingfisherManager.swift +++ b/Kingfisher/KingfisherManager.swift @@ -37,8 +37,8 @@ public class RetrieveImageTask { // the download task should not begin. var cancelledBeforeDownlodStarting: Bool = false - var diskRetrieveTask: RetrieveImageDiskTask? - var downloadTask: RetrieveImageDownloadTask? + public var diskRetrieveTask: RetrieveImageDiskTask? + public var downloadTask: RetrieveImageDownloadTask? /** Cancel current task. If this task does not begin or already done, do nothing. diff --git a/README.md b/README.md index 373742975..d2adbc55e 100644 --- a/README.md +++ b/README.md @@ -58,7 +58,7 @@ source 'https://github.com/CocoaPods/Specs.git' platform :ios, '8.0' use_frameworks! -pod 'Kingfisher', '~> 1.8' +pod 'Kingfisher', '~> 1.9' ``` Then, run the following command: @@ -83,7 +83,7 @@ $ brew install carthage To integrate Kingfisher into your Xcode project using Carthage, specify it in your `Cartfile`: ``` ogdl -github "onevcat/Kingfisher" ~> 1.8 +github "onevcat/Kingfisher" ~> 1.9 ``` Then, run the following command to build the Kingfisher framework: @@ -215,16 +215,28 @@ imageView.kf_setImageWithURL(NSURL(string: "your_image_url")!, #### Cancel Task -All `kf_setImageWithURL` methods return a `RetrieveImageTask` object. You can `cancel` the task if the images are not needed. +You can `cancel` the task if the images are not needed anymore. +It could be useful when you use Kingfisher to set an image in a cell of table view or collection view, +but users scroll the view and the cells disappeared before downloading finishing. ``` swift -let task = imageView.kf_setImageWithURL(NSURL(string: "http://your_image_url.png")!) -task.cancel() +imageView.kf_setImageWithURL(NSURL(string: "http://your_image_url.png")!) // The image retrieving will stop. +imageView.kf_cancelDownloadTask() ``` -There is a category for `UIButton` as well. +All `kf_setImageWithURL` methods return a `RetrieveImageTask` object as well. You can also hold and manage it if you need to apply some check: + +``` swift +let task = imageView.kf_setImageWithURL(NSURL(string: "http://your_image_url.png")!) + +let urlShouldNotBeCancelled: URL = ... + +if task.downloadTask?.URL != urlShouldNotBeCancelled { + task.cancel() +} +``` ### Downloader & Cache system