瀏覽代碼

Allow replacing of the progressive image

onevcat 3 年之前
父節點
當前提交
de48154e7a
共有 2 個文件被更改,包括 25 次插入4 次删除
  1. 11 3
      Sources/General/KingfisherManager.swift
  2. 14 1
      Sources/Image/ImageProgressive.swift

+ 11 - 3
Sources/General/KingfisherManager.swift

@@ -221,14 +221,22 @@ public class KingfisherManager {
         with source: Source,
         options: KingfisherParsedOptionsInfo,
         downloadTaskUpdated: DownloadTaskUpdatedBlock? = nil,
-        progressiveImageSetter: ((KFCrossPlatformImage) -> Void)? = nil,
+        progressiveImageSetter: ((KFCrossPlatformImage?) -> Void)? = nil,
         referenceTaskIdentifierChecker: (() -> Bool)? = nil,
         completionHandler: ((Result<RetrieveImageResult, KingfisherError>) -> Void)?) -> DownloadTask?
     {
         var options = options
         if let provider = ImageProgressiveProvider(options, refresh: { image in
-            options.progressiveJPEG?.onImageUpdated(image)
-            progressiveImageSetter?(image)
+            guard let strategy = options.progressiveJPEG?.onImageUpdated(image) else {
+                return
+            }
+            guard let setter = progressiveImageSetter else {
+                return
+            }
+            switch strategy {
+            case .keep: setter(image)
+            case .replace(let newImage): setter(newImage)
+            }
         }) {
             options.onDataReceived = (options.onDataReceived ?? []) + [provider]
         }

+ 14 - 1
Sources/Image/ImageProgressive.swift

@@ -32,6 +32,16 @@ private let sharedProcessingQueue: CallbackQueue =
 
 public struct ImageProgressive {
     
+    /// The updating strategy when an intermediate progressive image is generated and about to be set to the hosting view.
+    ///
+    /// - keep: Do not change the received intermediate progressive image. Just use as it is.
+    /// - replace: Replace the image to a new one. If the progressive loading is initialized by a view extension in
+    ///            Kingfisher, the replacing image will be used to update the view.
+    public enum UpdatingStrategy {
+        case keep
+        case replace(KFCrossPlatformImage?)
+    }
+    
     /// A default `ImageProgressive` could be used across. It blurs the progressive loading with the fastest
     /// scan enabled and scan interval as 0.
     public static let `default` = ImageProgressive(
@@ -47,7 +57,10 @@ public struct ImageProgressive {
     /// Minimum time interval for each scan
     let scanInterval: TimeInterval
     
-    public let onImageUpdated = Delegate<KFCrossPlatformImage, Void>()
+    /// Called when an intermediate image is prepared and about to be set to the image view. The return value of this
+    /// delegate will be used to update the hosting view, if any. Otherwise, if there is no hosting view (a.k.a the
+    /// image retrieving is not happening from a view extension method), the returned `UpdatingStrategy` is ignored.
+    public let onImageUpdated = Delegate<KFCrossPlatformImage, UpdatingStrategy>()
     
     public init(isBlur: Bool,
                 isFastestScan: Bool,