Browse Source

Move and apply onFailureImage to common scope

onevcat 4 năm trước cách đây
mục cha
commit
1438c76c10

+ 0 - 13
Sources/General/KF.swift

@@ -363,19 +363,6 @@ extension KF.Builder {
         return self
     }
 
-    /// Sets the image that will be used if an image retrieving task fails.
-    /// - Parameter image: The image that will be used when something goes wrong.
-    /// - Returns: A `KF.Builder` with changes applied.
-    ///
-    /// If set and an image retrieving error occurred Kingfisher will set provided image (or empty)
-    /// in place of requested one. It's useful when you don't want to show placeholder
-    /// during loading time but wants to use some default image when requests will be failed.
-    ///
-    public func onFailureImage(_ image: KFCrossPlatformImage?) -> Self {
-        options.onFailureImage = .some(image)
-        return self
-    }
-
     /// Enables progressive image loading with a specified `ImageProgressive` setting to process the
     /// progressive JPEG data and display it in a progressive way.
     /// - Parameter progressive: The progressive settings which is used while loading.

+ 13 - 1
Sources/General/KFOptionsSetter.swift

@@ -326,12 +326,24 @@ extension KFOptionSetter {
 
     /// Sets whether the image setting for an image view should happen with transition even when retrieved from cache.
     /// - Parameter enabled: Enable the force transition or not.
-    /// - Returns: A `KF.Builder` with changes applied.
+    /// - Returns: A `Self` with changes applied.
     public func forceTransition(_ enabled: Bool = true) -> Self {
         options.forceTransition = enabled
         return self
     }
 
+    /// Sets the image that will be used if an image retrieving task fails.
+    /// - Parameter image: The image that will be used when something goes wrong.
+    /// - Returns: A `Self` with changes applied.
+    ///
+    /// If set and an image retrieving error occurred Kingfisher will set provided image (or empty)
+    /// in place of requested one. It's useful when you don't want to show placeholder
+    /// during loading time but wants to use some default image when requests will be failed.
+    ///
+    public func onFailureImage(_ image: KFCrossPlatformImage?) -> Self {
+        options.onFailureImage = .some(image)
+        return self
+    }
 }
 
 // MARK: - Request Modifier

+ 7 - 0
Sources/SwiftUI/ImageBinder.swift

@@ -86,6 +86,13 @@ extension KFImage {
                                 context.onSuccessDelegate.call(value)
                             }
                         case .failure(let error):
+                            CallbackQueue.mainCurrentOrAsync.execute {
+                                if let image = context.options.onFailureImage {
+                                    self.loadedImage = image
+                                }
+                                self.loaded = true
+                            }
+                            
                             CallbackQueue.mainAsync.execute {
                                 context.onFailureDelegate.call(error)
                             }