利用 Kingfisher 处理图片

  • 基础配置
func KingfisherDefaultOptionsInfoItem() -> [KingfisherOptionsInfoItem] {
    return [.transition(.fade(0.2)), .keepCurrentImageWhileLoading]
}
  • 圆角处理
extension UIImageView {
    func setNetImgWithCornerRadius(
        urlString: String,
        placeholder:UIImage?,
        targetSize: CGSize,
        cornerRadius: CGFloat,
        roundingCorners: RectCorner = .all,
        backgroundColor: UIColor = .clear,
        priority: Float = URLSessionTask.defaultPriority) {
        
        var options = KingfisherDefaultOptionsInfoItem()
        options.append(.downloadPriority(priority))
        let resize = ResizingImageProcessor(referenceSize: targetSize, mode: .aspectFill)
        let crop = CroppingImageProcessor(size: targetSize)
        let round = RoundCornerImageProcessor(cornerRadius: cornerRadius, roundingCorners: roundingCorners, backgroundColor: backgroundColor)
        let processor = (resize >> crop) >> round
        options.append(.processor(processor))
        
        let url = URL(string: urlString)
        kf.setImage(with: url, placeholder: placeholder, options: options, progressBlock: nil, completionHandler: nil)
    }
}
  • 毛玻璃特效
extension UIImageView {
    func setNetImgWithBlur(urlString: String,
                           placeholder:UIImage?,
                           blurRadius: CGFloat,
                           tintColor: UIColor,
                           priority: Float = URLSessionTask.defaultPriority) {
        
        var options = KingfisherDefaultOptionsInfoItem()
        options.append(.downloadPriority(priority))
        
        let processor = BlurImageProcessor(blurRadius: blurRadius)
        let processor2 = TintImageProcessor(tint: tintColor)
        options.append(.processor(processor >> processor2))
        
        let url = URL(string: urlString)
        kf.setImage(with: url,
                         placeholder: placeholder,
                         options: options,
                         progressBlock: nil,
                         completionHandler: nil)
    }
}