Sfoglia il codice sorgente

Flag that image was downloaded

Igor Nikitin 9 anni fa
parent
commit
d06a357476

+ 1 - 1
Sources/ImagePrefetcher.swift

@@ -199,7 +199,7 @@ public class ImagePrefetcher {
             retrieveImageTask: task,
             progressBlock: nil,
             completionHandler: {
-                (image, error, _, _) -> () in
+                (image, error, _, _, _) -> () in
                 
                 self.tasks.removeValue(forKey: resource.downloadURL)
                 

+ 1 - 1
Sources/ImageView+Kingfisher.swift

@@ -120,7 +120,7 @@ extension ImageView {
                     progressBlock(receivedSize: receivedSize, totalSize: totalSize)
                 }
             },
-            completionHandler: {[weak self] image, error, cacheType, imageURL in
+            completionHandler: {[weak self] image, error, cacheType, _, imageURL in
                 
                 dispatch_async_safely_to_main_queue {
                     guard let sSelf = self, imageURL == sSelf.kf_webURL else {

+ 11 - 9
Sources/KingfisherManager.swift

@@ -32,6 +32,7 @@ import UIKit
 
 public typealias DownloadProgressBlock = ((receivedSize: Int64, totalSize: Int64) -> ())
 public typealias CompletionHandler = ((image: Image?, error: NSError?, cacheType: CacheType, imageURL: URL?) -> ())
+public typealias RetrieveCompletionHandler = ((image: Image?, error: NSError?, cacheType: CacheType, wasDownloaded: Bool, imageURL: URL?) -> ())
 
 /// RetrieveImageTask represents a task of image retrieving process.
 /// It contains an async task of getting image from disk and from network.
@@ -119,7 +120,7 @@ public class KingfisherManager {
     public func retrieveImageWithResource(_ resource: Resource,
         optionsInfo: KingfisherOptionsInfo?,
         progressBlock: DownloadProgressBlock?,
-        completionHandler: CompletionHandler?) -> RetrieveImageTask
+        completionHandler: RetrieveCompletionHandler?) -> RetrieveImageTask
     {
         let task = RetrieveImageTask()
 
@@ -162,7 +163,7 @@ public class KingfisherManager {
     public func retrieveImageWithURL(_ URL: Foundation.URL,
                              optionsInfo: KingfisherOptionsInfo?,
                            progressBlock: DownloadProgressBlock?,
-                       completionHandler: CompletionHandler?) -> RetrieveImageTask
+                       completionHandler: RetrieveCompletionHandler?) -> RetrieveImageTask
     {
         return retrieveImageWithResource(Resource(downloadURL: URL), optionsInfo: optionsInfo, progressBlock: progressBlock, completionHandler: completionHandler)
     }
@@ -172,7 +173,7 @@ public class KingfisherManager {
                                forKey key: String,
                         retrieveImageTask: RetrieveImageTask,
                             progressBlock: DownloadProgressBlock?,
-                        completionHandler: CompletionHandler?,
+                        completionHandler: RetrieveCompletionHandler?,
                                   options: KingfisherOptionsInfo?) -> RetrieveImageDownloadTask?
     {
         let downloader = options?.downloader ?? self.downloader
@@ -187,7 +188,7 @@ public class KingfisherManager {
                     // Not modified. Try to find the image from cache.
                     // (The image should be in cache. It should be guaranteed by the framework users.)
                     targetCache.retrieveImageForKey(key, options: options, completionHandler: { (cacheImage, cacheType) -> () in
-                        completionHandler?(image: cacheImage, error: nil, cacheType: cacheType, imageURL: URL)
+                        completionHandler?(image: cacheImage, error: nil, cacheType: cacheType, wasDownloaded: true, imageURL: URL)
                         
                     })
                     return
@@ -197,7 +198,7 @@ public class KingfisherManager {
                     targetCache.storeImage(image, originalData: originalData, forKey: key, toDisk: !(options?.cacheMemoryOnly ?? false), completionHandler: nil)
                 }
 
-                completionHandler?(image: image, error: error, cacheType: .none, imageURL: URL)
+                completionHandler?(image: image, error: error, cacheType: .none, wasDownloaded: true, imageURL: URL)
 
             })
     }
@@ -206,20 +207,21 @@ public class KingfisherManager {
                                    withURL URL: Foundation.URL,
                              retrieveImageTask: RetrieveImageTask,
                                  progressBlock: DownloadProgressBlock?,
-                             completionHandler: CompletionHandler?,
+                             completionHandler: RetrieveCompletionHandler?,
                                        options: KingfisherOptionsInfo?)
     {
-        let diskTaskCompletionHandler: CompletionHandler = { (image, error, cacheType, imageURL) -> () in
+        let diskTaskCompletionHandler: RetrieveCompletionHandler = {
+            (image, error, cacheType, wasDownloaded, imageURL) -> () in
             // Break retain cycle created inside diskTask closure below
             retrieveImageTask.diskRetrieveTask = nil
-            completionHandler?(image: image, error: error, cacheType: cacheType, imageURL: imageURL)
+            completionHandler?(image: image, error: error, cacheType: cacheType, wasDownloaded: wasDownloaded, imageURL: imageURL)
         }
         
         let targetCache = options?.targetCache ?? cache
         let diskTask = targetCache.retrieveImageForKey(key, options: options,
             completionHandler: { image, cacheType in
                 if image != nil {
-                    diskTaskCompletionHandler(image: image, error: nil, cacheType:cacheType, imageURL: URL)
+                    diskTaskCompletionHandler(image: image, error: nil, cacheType:cacheType, wasDownloaded: false, imageURL: URL)
                 } else {
                     self.downloadAndCacheImageWithURL(URL,
                         forKey: key,

+ 2 - 2
Sources/UIButton+Kingfisher.swift

@@ -100,7 +100,7 @@ extension UIButton {
                     progressBlock(receivedSize: receivedSize, totalSize: totalSize)
                 }
             },
-            completionHandler: {[weak self] image, error, cacheType, imageURL in
+            completionHandler: {[weak self] image, error, cacheType, _, imageURL in
                 dispatch_async_safely_to_main_queue {
                     guard let sSelf = self, imageURL == sSelf.kf_webURLForState(state) else {
                         return
@@ -239,7 +239,7 @@ extension UIButton {
                     progressBlock(receivedSize: receivedSize, totalSize: totalSize)
                 }
             },
-            completionHandler: { [weak self] image, error, cacheType, imageURL in
+            completionHandler: { [weak self] image, error, cacheType, _, imageURL in
                 dispatch_async_safely_to_main_queue {
                     guard let sSelf = self, imageURL == sSelf.kf_backgroundWebURLForState(state) else {
                         return