当前位置:网站首页>扩展UIImageView

扩展UIImageView

2020-12-06 11:14:06 剑龙御武

//
//  WYImageView+Kingfisher.swift
//  DYWX
//
//  Created by 王武 on 2020/11/24.
//

import UIKit
import Kingfisher

extension UIImageView {
    public var sp: UIImageView {
        get { return self }
    }
    
    /// 1.加载网络图片
    ///
    /// - Parameters:
    ///   - urlStr: 图片 URL地址
    ///   - placeholder: 占位图
    func wy_setImage(urlStr: String,  placeholder: UIImage? = nil) {
        guard let url = URL(string: urlStr) else {
            print("url:|\(urlStr)|无法解析为URL类型")
            if let placeholder = placeholder {
                self.image = placeholder
            }
            return
        }
        kf.setImage(with: url, placeholder: placeholder, options: [.backgroundDecode], progressBlock: nil, completionHandler: nil)
    }
    
    /// 2.加载网络图片
    ///
    /// - Parameters:
    ///   - urlStr: 图片地址
    ///   - placeholder: 占位图
    ///   - progresseCellBack: 加载进度
    ///   - completionHandler: 图片加载完毕完成回调
    func wy_setImageWithBlock(urlStr : String, placeholder: UIImage = UIImage(),progresseCellBack: DownloadProgressBlock? = nil,completionHandler: CompletionHandler?) {
        guard let url = URL(string: urlStr) else {
            print("url:|\(urlStr)|无法解析为URL类型")
            self.image = placeholder
            return
        }
        kf.setImage(with: url, placeholder: placeholder, options: [.backgroundDecode], progressBlock: { (receivedSize, totalSize) in
            progresseCellBack?(receivedSize,totalSize)
        }, completionHandler: { (_ image: Image?, _ error: NSError?, _ cacheType: CacheType, _ imageURL: URL?) in
            completionHandler?(image, error, cacheType, imageURL)
        })
    }
}

版权声明
本文为[剑龙御武]所创,转载请带上原文链接,感谢
https://my.oschina.net/wwyywg/blog/4776739