美文网首页
利用 Kingfisher 处理图片

利用 Kingfisher 处理图片

作者: Civel_Xu | 来源:发表于2019-05-16 17:23 被阅读0次
  • 基础配置
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)
    }
}

相关文章

网友评论

      本文标题:利用 Kingfisher 处理图片

      本文链接:https://www.haomeiwen.com/subject/qxfwaqtx.html