利用 Kingfisher 处理图片

  • 基础配置
    1
    2
    3
    4
    5
    func KingfisherDefaultOptionsInfoItem() -> [KingfisherOptionsInfoItem] {
    return [.transition(.fade(0.2)),
    .keepCurrentImageWhileLoading,
    .cacheSerializer(FormatIndicatedCacheSerializer.png)]
    }
  • 圆角处理

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    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.init(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)
    self.kf.setImage(with: url,
    placeholder: placeholder,
    options: options,
    progressBlock: nil,
    completionHandler: nil)
    }
    }
  • 毛玻璃特效

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    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)
    self.kf.setImage(with: url,
    placeholder: placeholder,
    options: options,
    progressBlock: nil,
    completionHandler: nil)
    }
    }
0%