Skip to content

Commit

Permalink
Call image modifier actually
Browse files Browse the repository at this point in the history
  • Loading branch information
onevcat committed Nov 30, 2017
1 parent b3d7e30 commit 0b9acd0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
23 changes: 16 additions & 7 deletions Sources/ImageModifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,24 @@ 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)

fileprivate struct GeneralModifier: ImageModifier {
let identifier: String
let m: ModifierImp
func modify(image: Image) -> Image {
func modify(_ image: Image) -> Image {
return m(image)
}
}
Expand All @@ -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
}
}
Expand All @@ -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)
}
}
Expand Down Expand Up @@ -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)
}
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
}
}
Expand Down
11 changes: 6 additions & 5 deletions Sources/KingfisherManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -186,7 +187,7 @@ public class KingfisherManager {
}
}

completionHandler?(image, error, .none, url)
completionHandler?(imageModifier.modify(image), error, .none, url)

})
}
Expand All @@ -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() {
Expand Down

0 comments on commit 0b9acd0

Please sign in to comment.