| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- //
- // ImageProcessor.swift
- // Kingfisher
- //
- // Created by WANG WEI on 2016/08/26.
- // Copyright © 2016年 Wei Wang. All rights reserved.
- //
- import Foundation
- import CoreGraphics
- public enum ImageProcessItem {
- case image(Image)
- case data(Data)
- }
- public protocol ImageProcessor {
- func process(item: ImageProcessItem, options: KingfisherOptionsInfo) -> Image?
- }
- typealias ProcessorImp = ((ImageProcessItem, KingfisherOptionsInfo) -> Image?)
- public extension ImageProcessor {
- func append(another: ImageProcessor) -> ImageProcessor {
- return GeneralProcessor { item, options in
- if let image = self.process(item: item, options: options) {
- return another.process(item: .image(image), options: options)
- } else {
- return nil
- }
- }
- }
- }
- fileprivate struct GeneralProcessor: ImageProcessor {
- let p: ProcessorImp
- func process(item: ImageProcessItem, options: KingfisherOptionsInfo) -> Image? {
- return p(item, options)
- }
- }
- public struct DefaultProcessor: ImageProcessor {
- public func process(item: ImageProcessItem, options: KingfisherOptionsInfo) -> Image? {
- switch item {
- case .image(let image):
- return image
- case .data(let data):
- return Image.kf_image(data: data, scale: options.scaleFactor, preloadAllGIFData: options.preloadAllGIFData)
- }
- }
- }
- public struct RoundCornerImageProcessor: ImageProcessor {
-
- public let cornerRadius: CGFloat
- public let targetSize: CGSize?
-
- public init(cornerRadius: CGFloat, targetSize: CGSize? = nil) {
- self.cornerRadius = cornerRadius
- self.targetSize = targetSize
- }
-
- public func process(item: ImageProcessItem, options: KingfisherOptionsInfo) -> Image? {
- switch item {
- case .image(let image):
- let size = targetSize ?? image.kf_size
- return image.kf_image(withRoundRadius: cornerRadius, fit: size, scale: options.scaleFactor)
- case .data(_):
- return (DefaultProcessor() |> self).process(item: item, options: options)
- }
- }
- }
- public struct ResizingImageProcessor: ImageProcessor {
- public let targetSize: CGSize
- public func process(item: ImageProcessItem, options: KingfisherOptionsInfo) -> Image? {
- switch item {
- case .image(let image):
- return image.kf_resize(to: targetSize)
- case .data(_):
- return (DefaultProcessor() |> self).process(item: item, options: options)
- }
- }
- }
- infix operator |>: DefaultPrecedence
- public func |>(left: ImageProcessor, right: ImageProcessor) -> ImageProcessor {
- return left.append(another: right)
- }
|