Kingfisher3.x的分析與學(xué)習(xí)(二)

本文的內(nèi)容主要以代碼為主搪泳,分析和學(xué)習(xí)了主要的類蚌父,在學(xué)習(xí)過程中添加相關(guān)的中文注釋,重要且利于理解的英文注釋也保留;如果有理解不對的地方结缚,請告訴我。

ImageProcessor

//圖片處理器软棺,定義

public enum ImageProcessItem {
    case image(Image)
    case data(Data)
}

/// An `ImageProcessor` would be used to convert some downloaded data to an image.
public protocol ImageProcessor {

    var identifier: String { get }
    
     func process(item: ImageProcessItem, options: KingfisherOptionsInfo) -> Image?
}

typealias ProcessorImp = ((ImageProcessItem, KingfisherOptionsInfo) -> Image?)

public extension ImageProcessor {
    
    /// Append an `ImageProcessor` to another. The identifier of the new `ImageProcessor` 
    /// will be "\(self.identifier)|>\(another.identifier)".
    ///
    /// - parameter another: An `ImageProcessor` you want to append to `self`.
    ///
    /// - returns: The new `ImageProcessor`. It will process the image in the order
    ///            of the two processors concatenated.
    public func append(another: ImageProcessor) -> ImageProcessor {
        let newIdentifier = identifier.appending("|>\(another.identifier)")
        return GeneralProcessor(identifier: newIdentifier) {
            item, options in
            //先由ImageProcessor轉(zhuǎn)換成圖片红竭,再交給another完成another的操作
            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 identifier: String
    let p: ProcessorImp
    func process(item: ImageProcessItem, options: KingfisherOptionsInfo) -> Image? {
        return p(item, options)
    }
}

/// The default processor. It convert the input data to a valid image.
/// Images of .PNG, .JPEG and .GIF format are supported.
/// If an image is given, `DefaultImageProcessor` will do nothing on it and just return that image.
public struct DefaultImageProcessor: ImageProcessor {
    
    /// A default `DefaultImageProcessor` could be used across.
    public static let `default` = DefaultImageProcessor()
    
    public let identifier = ""
    
    /// Initialize a `DefaultImageProcessor`
    ///
    /// - returns: An initialized `DefaultImageProcessor`.
    public init() {}
    //圖片處理器
    public func process(item: ImageProcessItem, options: KingfisherOptionsInfo) -> Image? {
        switch item {
        case .image(let image):
            return image
        case .data(let data):
            return Kingfisher<Image>.image(
                data: data,
                scale: options.scaleFactor,
                preloadAllGIFData: options.preloadAllGIFData,
                onlyFirstFrame: options.onlyLoadFirstFrame)
        }
    }
}

圖片處理器中的具體功能都在Image類中實(shí)現(xiàn),他們都遵守并實(shí)現(xiàn)了ImageProcessor

//圓角
public struct RoundCornerImageProcessor: ImageProcessor {
    public let identifier: String

    //輸出圖片的尺寸
    public let targetSize: CGSize?

    public init(cornerRadius: CGFloat, targetSize: CGSize? = nil) {
        self.cornerRadius = cornerRadius
        self.targetSize = targetSize
        if let size = targetSize {
            self.identifier = "com.onevcat.Kingfisher.RoundCornerImageProcessor(\(cornerRadius)_\(size))"
        } else {
            self.identifier = "com.onevcat.Kingfisher.RoundCornerImageProcessor(\(cornerRadius))"
        }
    }
    
    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)
        case .data(_):
            //先轉(zhuǎn)換為image喘落,再做圓角處理
            return (DefaultImageProcessor.default >> self).process(item: item, options: options)
        }
    }
}
//resize
ResizingImageProcessor
 //模糊圖片
 BlurImageProcessor
 //顏色覆蓋
 OverlayImageProcessor
 //tint
 TintImageProcessor
 //圖片亮度茵宪、對比度等設(shè)置
 ColorControlsProcessor
 

image

關(guān)聯(lián)或者別名新屬性

// MARK: - Image Properties
extension Kingfisher where Base: Image {
    //動態(tài)添加一個animatedImageData屬性
    fileprivate(set) var animatedImageData: Data? {
        get {
            return objc_getAssociatedObject(base, &animatedImageDataKey) as? Data
        }
        set {
            //OBJC_ASSOCIATION_RETAIN_NONATOMIC,強(qiáng)引用關(guān)聯(lián)且非原子操作
            objc_setAssociatedObject(base, &animatedImageDataKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }

    var cgImage: CGImage? {
        return base.cgImage
    }
    
    var scale: CGFloat {
        return base.scale
    }
    
    var images: [Image]? {
        return base.images
    }
    
    var duration: TimeInterval {
        return base.duration
    }
    
    fileprivate(set) var imageSource: ImageSource? {
        get {
            return objc_getAssociatedObject(base, &imageSourceKey) as? ImageSource
        }
        set {
            objc_setAssociatedObject(base, &imageSourceKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }
    
    var size: CGSize {
        return base.size
    }
   
}

重新繪制Kingfisher中定義的unknow的image

public var normalized: Image {
        // prevent animated image (GIF) lose it's images
        guard images == nil else { return base }
        // No need to do anything if already up
        guard base.imageOrientation != .up else { return base }
    
        return draw(cgImage: nil, to: size) {
            base.draw(in: CGRect(origin: CGPoint.zero, size: size))
        }
    }
    

image中data與image的相互轉(zhuǎn)化

// MARK: - Image Representation
extension Kingfisher where Base: Image {
    // MARK: - PNG
    public func pngRepresentation() -> Data? {
            //轉(zhuǎn)換PNG格式image為data
            return UIImagePNGRepresentation(base)
    }
    
    // MARK: - JPEG
    public func jpegRepresentation(compressionQuality: CGFloat) -> Data? {
            //轉(zhuǎn)換JPEG格式image為data,compressionQuality為壓縮比例
            return UIImageJPEGRepresentation(base, compressionQuality)
    }
    
    // MARK: - GIF
    public func gifRepresentation() -> Data? {
        return animatedImageData
    }
}

    static func image(data: Data, scale: CGFloat, preloadAllGIFData: Bool, onlyFirstFrame: Bool) -> Image? {
        var image: Image?
            switch data.kf.imageFormat {
            case .JPEG:
                image = Image(data: data, scale: scale)
            case .PNG:
                image = Image(data: data, scale: scale)
            case .GIF:
                image = Kingfisher<Image>.animated(
                    with: data,
                    scale: scale,
                    duration: 0.0,
                    preloadAll: preloadAllGIFData,
                    onlyFirstFrame: onlyFirstFrame)
            case .unknown:
                image = Image(data: data, scale: scale)
            }
        return image
    }

圖片處理

//圓角
public func image(withRoundRadius radius: CGFloat, fit size: CGSize) -> Image 
//resize
 public func resize(to size: CGSize) -> Image 
 public func resize(to size: CGSize, for contentMode: ContentMode) -> Image
 
 //模糊圖片
 public func blurred(withRadius radius: CGFloat) -> Image
 //顏色覆蓋
 public func overlaying(with color: Color, fraction: CGFloat) -> Image
 //tint
 public func tinted(with color: Color) -> Image
 //圖片亮度、對比度等設(shè)置
 public func adjusted(brightness: CGFloat, contrast: CGFloat, saturation: CGFloat, inputEV: CGFloat) -> Image
 

解碼

    var decoded: Image? {
        return decoded(scale: scale)
    }
    
    func decoded(scale: CGFloat) -> Image {
            if images != nil { return base }
        
        guard let imageRef = self.cgImage else {
            assertionFailure("[Kingfisher] Decoding only works for CG-based image.")
            return base
        }
        let colorSpace = CGColorSpaceCreateDeviceRGB()
        guard let context = beginContext() else {
            assertionFailure("[Kingfisher] Decoding fails to create a valid context.")
            return base
        }
        
        defer { endContext() }
        
        let rect = CGRect(x: 0, y: 0, width: imageRef.width, height: imageRef.height)
        context.draw(imageRef, in: rect)
        let decompressedImageRef = context.makeImage()
        return Kingfisher<Image>.image(cgImage: decompressedImageRef!, scale: scale, refImage: base)
    }

圖片格式和size

/// Reference the source image reference
class ImageSource {
    var imageRef: CGImageSource?
    init(ref: CGImageSource) {
        self.imageRef = ref
    }
}

// MARK: - Image format
private struct ImageHeaderData {
    static var PNG: [UInt8] = [0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A]
    static var JPEG_SOI: [UInt8] = [0xFF, 0xD8]
    static var JPEG_IF: [UInt8] = [0xFF]
    static var GIF: [UInt8] = [0x47, 0x49, 0x46]
}

enum ImageFormat {
    case unknown, PNG, JPEG, GIF
}


// MARK: - Misc Helpers
public struct DataProxy {
    fileprivate let base: Data
    init(proxy: Data) {
        base = proxy
    }
}

extension Data: KingfisherCompatible {
    public typealias CompatibleType = DataProxy
    public var kf: DataProxy {
        return DataProxy(proxy: self)
    }
}

extension DataProxy {
    //返回image格式
    var imageFormat: ImageFormat {
        var buffer = [UInt8](repeating: 0, count: 8)
        (base as NSData).getBytes(&buffer, length: 8)
        if buffer == ImageHeaderData.PNG {
            return .PNG
        } else if buffer[0] == ImageHeaderData.JPEG_SOI[0] &&
            buffer[1] == ImageHeaderData.JPEG_SOI[1] &&
            buffer[2] == ImageHeaderData.JPEG_IF[0]
        {
            return .JPEG
        } else if buffer[0] == ImageHeaderData.GIF[0] &&
            buffer[1] == ImageHeaderData.GIF[1] &&
            buffer[2] == ImageHeaderData.GIF[2]
        {
            return .GIF
        }

        return .unknown
    }
}

public struct CGSizeProxy {
    fileprivate let base: CGSize
    init(proxy: CGSize) {
        base = proxy
    }
}

extension CGSize: KingfisherCompatible {
    public typealias CompatibleType = CGSizeProxy
    public var kf: CGSizeProxy {
        return CGSizeProxy(proxy: self)
    }
}

extension CGSizeProxy {
    func constrained(_ size: CGSize) -> CGSize {
        let aspectWidth = round(aspectRatio * size.height)
        let aspectHeight = round(size.width / aspectRatio)

        return aspectWidth > size.width ? CGSize(width: size.width, height: aspectHeight) : CGSize(width: aspectWidth, height: size.height)
    }

    func filling(_ size: CGSize) -> CGSize {
        let aspectWidth = round(aspectRatio * size.height)
        let aspectHeight = round(size.width / aspectRatio)

        return aspectWidth < size.width ? CGSize(width: size.width, height: aspectHeight) : CGSize(width: aspectWidth, height: size.height)
    }

    private var aspectRatio: CGFloat {
        return base.height == 0.0 ? 1.0 : base.width / base.height
    }
}

Indicator

//協(xié)議和枚舉瘦棋,默認(rèn)的就是none稀火。可以看到能支持系統(tǒng)的UIActivityIndicatorView赌朋,
//還能支持gif凰狞,當(dāng)然要傳入一個imageData的參數(shù),還能支持自定義的Indicator沛慢。從代碼
//中可以看的出來Indicator是一個協(xié)議赡若,如果要實(shí)現(xiàn)自定義只需要繼承這個協(xié)議就可以了
public enum IndicatorType {
    /// No indicator.
    case none
    /// Use system activity indicator.
    case activity
    /// Use an image as indicator. GIF is supported.
    case image(imageData: Data)
    /// Use a custom indicator, which conforms to the `Indicator` protocol.
    case custom(indicator: Indicator)
}

// MARK: - Indicator Protocol
public protocol Indicator {
    func startAnimatingView()
    func stopAnimatingView()

    var viewCenter: CGPoint { get set }
    var view: IndicatorView { get }
}

extension Indicator {
    public var viewCenter: CGPoint {
        get {
            return view.center
        }
        set {
            view.center = newValue
        }
    }
}

遵守Indicator Protocol的具體實(shí)現(xiàn)

// MARK: - ActivityIndicator
// Displays a NSProgressIndicator / UIActivityIndicatorView
struct ActivityIndicator: Indicator {

    private let activityIndicatorView: UIActivityIndicatorView

    var view: IndicatorView {
        return activityIndicatorView
    }

    func startAnimatingView() {
        activityIndicatorView.startAnimating()
        activityIndicatorView.isHidden = false
    }

    func stopAnimatingView() {
        activityIndicatorView.stopAnimating()
        activityIndicatorView.isHidden = true
    }

    init() {
            let indicatorStyle = UIActivityIndicatorViewStyle.gray
            activityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle:indicatorStyle)
            activityIndicatorView.autoresizingMask = [.flexibleLeftMargin, .flexibleRightMargin, .flexibleBottomMargin, .flexibleTopMargin]
    }
}

// MARK: - ImageIndicator
// Displays an ImageView. Supports gif
struct ImageIndicator: Indicator {
    private let animatedImageIndicatorView: ImageView

    var view: IndicatorView {
        return animatedImageIndicatorView
    }

    init?(imageData data: Data, processor: ImageProcessor = DefaultImageProcessor.default, options: KingfisherOptionsInfo = KingfisherEmptyOptionsInfo) {

        var options = options
        // Use normal image view to show gif, so we need to preload all gif data.
        if !options.preloadAllGIFData {
            options.append(.preloadAllGIFData)
        }
        
        guard let image = processor.process(item: .data(data), options: options) else {
            return nil
        }

        animatedImageIndicatorView = ImageView()
        animatedImageIndicatorView.image = image
        
         animatedImageIndicatorView.contentMode = .center
            
        animatedImageIndicatorView.autoresizingMask = [.flexibleLeftMargin,
                                                           .flexibleRightMargin,
                                                           .flexibleBottomMargin,
                                                           .flexibleTopMargin]
    }

    func startAnimatingView() {
           animatedImageIndicatorView.startAnimating()

        animatedImageIndicatorView.isHidden = false
    }

    func stopAnimatingView() {

        animatedImageIndicatorView.stopAnimating()
        animatedImageIndicatorView.isHidden = true
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市团甲,隨后出現(xiàn)的幾起案子逾冬,更是在濱河造成了極大的恐慌,老刑警劉巖躺苦,帶你破解...
    沈念sama閱讀 212,718評論 6 492
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件身腻,死亡現(xiàn)場離奇詭異,居然都是意外死亡圾另,警方通過查閱死者的電腦和手機(jī)霸株,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,683評論 3 385
  • 文/潘曉璐 我一進(jìn)店門雕沉,熙熙樓的掌柜王于貴愁眉苦臉地迎上來集乔,“玉大人,你說我怎么就攤上這事坡椒∪怕罚” “怎么了?”我有些...
    開封第一講書人閱讀 158,207評論 0 348
  • 文/不壞的土叔 我叫張陵倔叼,是天一觀的道長汗唱。 經(jīng)常有香客問我,道長丈攒,這世上最難降的妖魔是什么哩罪? 我笑而不...
    開封第一講書人閱讀 56,755評論 1 284
  • 正文 為了忘掉前任授霸,我火速辦了婚禮,結(jié)果婚禮上际插,老公的妹妹穿的比我還像新娘碘耳。我一直安慰自己,他們只是感情好框弛,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,862評論 6 386
  • 文/花漫 我一把揭開白布辛辨。 她就那樣靜靜地躺著,像睡著了一般瑟枫。 火紅的嫁衣襯著肌膚如雪斗搞。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 50,050評論 1 291
  • 那天慷妙,我揣著相機(jī)與錄音僻焚,去河邊找鬼。 笑死景殷,一個胖子當(dāng)著我的面吹牛溅呢,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播猿挚,決...
    沈念sama閱讀 39,136評論 3 410
  • 文/蒼蘭香墨 我猛地睜開眼咐旧,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了绩蜻?” 一聲冷哼從身側(cè)響起铣墨,我...
    開封第一講書人閱讀 37,882評論 0 268
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎办绝,沒想到半個月后伊约,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 44,330評論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡孕蝉,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,651評論 2 327
  • 正文 我和宋清朗相戀三年屡律,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片降淮。...
    茶點(diǎn)故事閱讀 38,789評論 1 341
  • 序言:一個原本活蹦亂跳的男人離奇死亡超埋,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出佳鳖,到底是詐尸還是另有隱情霍殴,我是刑警寧澤,帶...
    沈念sama閱讀 34,477評論 4 333
  • 正文 年R本政府宣布系吩,位于F島的核電站来庭,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏穿挨。R本人自食惡果不足惜月弛,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 40,135評論 3 317
  • 文/蒙蒙 一肴盏、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧帽衙,春花似錦叁鉴、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,864評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至冀泻,卻和暖如春常侣,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背弹渔。 一陣腳步聲響...
    開封第一講書人閱讀 32,099評論 1 267
  • 我被黑心中介騙來泰國打工胳施, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人肢专。 一個月前我還...
    沈念sama閱讀 46,598評論 2 362
  • 正文 我出身青樓舞肆,卻偏偏與公主長得像,于是被迫代替她去往敵國和親博杖。 傳聞我的和親對象是個殘疾皇子椿胯,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,697評論 2 351

推薦閱讀更多精彩內(nèi)容