onevcat 8 лет назад
Родитель
Сommit
5c0072a0e5
2 измененных файлов с 22 добавлено и 12 удалено
  1. 16 7
      Sources/ImageModifier.swift
  2. 6 5
      Sources/KingfisherManager.swift

+ 16 - 7
Sources/ImageModifier.swift

@@ -38,7 +38,16 @@ public protocol ImageModifier {
     /// - Note: The return value will be unmodified if modifying is not possible on
     ///         the current platform.
     /// - Note: Most modifiers support UIImage or NSImage, but not CGImage.
-    func modify(image: Image) -> Image
+    func modify(_ image: Image) -> Image
+}
+
+extension ImageModifier {
+    func modify(_ image: Image?) -> Image? {
+        guard let image = image else {
+            return nil
+        }
+        return modify(image)
+    }
 }
 
 typealias ModifierImp = ((Image) -> Image)
@@ -46,7 +55,7 @@ typealias ModifierImp = ((Image) -> Image)
 fileprivate struct GeneralModifier: ImageModifier {
     let identifier: String
     let m: ModifierImp
-    func modify(image: Image) -> Image {
+    func modify(_ image: Image) -> Image {
         return m(image)
     }
 }
@@ -68,7 +77,7 @@ public struct DefaultImageModifier: ImageModifier {
     /// - returns: The modified image.
     ///
     /// - Note: See documentation of `ImageModifier` protocol for more.
-    public func modify(image: Image) -> Image {
+    public func modify(_ image: Image) -> Image {
         return image
     }
 }
@@ -93,7 +102,7 @@ public struct AnyImageModifier: ImageModifier {
     /// - returns: The modified image.
     ///
     /// - Note: See documentation of `ImageModifier` protocol for more.
-    public func modify(image: Image) -> Image {
+    public func modify(_ image: Image) -> Image {
         return block(image)
     }
 }
@@ -124,7 +133,7 @@ import UIKit
         /// - returns: The modified image.
         ///
         /// - Note: See documentation of `ImageModifier` protocol for more.
-        public func modify(image: Image) -> Image {
+        public func modify(_ image: Image) -> Image {
             return image.withRenderingMode(renderingMode)
         }
     }
@@ -146,7 +155,7 @@ import UIKit
         /// - returns: The modified image.
         ///
         /// - Note: See documentation of `ImageModifier` protocol for more.
-        public func modify(image: Image) -> Image {
+        public func modify(_ image: Image) -> Image {
             if #available(iOS 9.0, *) {
                 return image.imageFlippedForRightToLeftLayoutDirection()
             } else {
@@ -175,7 +184,7 @@ import UIKit
         /// - returns: The modified image.
         ///
         /// - Note: See documentation of `ImageModifier` protocol for more.
-        public func modify(image: Image) -> Image {
+        public func modify(_ image: Image) -> Image {
             return image.withAlignmentRectInsets(alignmentInsets)
         }
     }

+ 6 - 5
Sources/KingfisherManager.swift

@@ -154,11 +154,12 @@ public class KingfisherManager {
             completionHandler: { image, error, imageURL, originalData in
 
                 let targetCache = options.targetCache
+                let imageModifier = options.imageModifier
                 if let error = error, error.code == KingfisherError.notModified.rawValue {
                     // Not modified. Try to find the image from cache.
                     // (The image should be in cache. It should be guaranteed by the framework users.)
                     targetCache.retrieveImage(forKey: key, options: options, completionHandler: { (cacheImage, cacheType) -> () in
-                        completionHandler?(cacheImage, nil, cacheType, url)
+                        completionHandler?(imageModifier.modify(cacheImage), nil, cacheType, url)
                     })
                     return
                 }
@@ -186,7 +187,7 @@ public class KingfisherManager {
                     }
                 }
 
-                completionHandler?(image, error, .none, url)
+                completionHandler?(imageModifier.modify(image), error, .none, url)
 
             })
     }
@@ -198,10 +199,10 @@ public class KingfisherManager {
                               completionHandler: CompletionHandler?,
                                         options: KingfisherOptionsInfo)
     {
-        
-        
+
         let diskTaskCompletionHandler: CompletionHandler = { (image, error, cacheType, imageURL) -> () in
-            completionHandler?(image, error, cacheType, imageURL)
+            let imageModifier = options.imageModifier
+            completionHandler?(imageModifier.modify(image), error, cacheType, imageURL)
         }
         
         func handleNoCache() {