From 0b9acd08e82389b0b243d50e862ac92b854e0141 Mon Sep 17 00:00:00 2001 From: onevcat Date: Thu, 30 Nov 2017 10:30:26 +0900 Subject: [PATCH] Call image modifier actually --- Sources/ImageModifier.swift | 23 ++++++++++++++++------- Sources/KingfisherManager.swift | 11 ++++++----- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/Sources/ImageModifier.swift b/Sources/ImageModifier.swift index 38e9f247b..53e02a86a 100644 --- a/Sources/ImageModifier.swift +++ b/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) } } diff --git a/Sources/KingfisherManager.swift b/Sources/KingfisherManager.swift index 85503f5c9..6166cbe8a 100755 --- a/Sources/KingfisherManager.swift +++ b/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() {