您好,登录后才能下订单哦!
import UIKit
import Kingfisher
private var lastURLKey: Void?
extension UIImageView {
public var fin_webURL: URL? {
return objc_getAssociatedObject(self, &lastURLKey) as? URL
}
fileprivate func fin_setWebURL(_ URL: Foundation.URL) {
objc_setAssociatedObject(self, &lastURLKey, URL, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
func fin_setImageWithUrl (_ URL: Foundation.URL ,placeholderImage: UIImage? = nil
,p_w_picpathModificationClosure:((_ p_w_picpath:UIImage) -> UIImage)? = nil){
self.p_w_picpath = placeholderImage
let resource = ImageResource(downloadURL: URL)
fin_setWebURL(resource.downloadURL)
KingfisherManager.shared.cache.retrieveImage(forKey: resource.cacheKey, options: nil) { (p_w_picpath, cacheType) -> () in
if p_w_picpath != nil {
dispatch_sync_safely_main_queue({ () -> () in
self.p_w_picpath = p_w_picpath
})
}
else {
KingfisherManager.shared.downloader.downloadImage(with: resource.downloadURL, options: nil, progressBlock: nil, completionHandler: { (p_w_picpath, error, p_w_picpathURL, originalData) -> () in
if let error = error , error.code == KingfisherError.notModified.rawValue {
KingfisherManager.shared.cache.retrieveImage(forKey: resource.cacheKey, options: nil, completionHandler: { (cacheImage, cacheType) -> () in
self.fin_setImage(cacheImage!, p_w_picpathURL: p_w_picpathURL!)
})
return
}
if var p_w_picpath = p_w_picpath, let originalData = originalData {
//处理图片
if let img = p_w_picpathModificationClosure?(p_w_picpath) {
p_w_picpath = img
}
//保存图片缓存
KingfisherManager.shared.cache.store(p_w_picpath, original: originalData, forKey: resource.cacheKey, toDisk: true, completionHandler: nil)
self.fin_setImage(p_w_picpath, p_w_picpathURL: p_w_picpathURL!)
}
})
}
}
}
fileprivate func fin_setImage(_ p_w_picpath:UIImage,p_w_picpathURL:URL) {
dispatch_sync_safely_main_queue { () -> () in
guard p_w_picpathURL == self.fin_webURL else {
return
}
self.p_w_picpath = p_w_picpath
}
}
}
func fin_defaultImageModification() -> ((_ p_w_picpath:UIImage) -> UIImage) {
return { ( p_w_picpath) -> UIImage in
let roundedImage = p_w_picpath.roundedCornerImageWithCornerRadius(3)
return roundedImage
}
}
//func dispatch_sync_safely_main_queue(_ block: ()->()) {
// if Thread.isMainThread {
// block()
// } else {
// DispatchQueue.main.sync {
// block()
// }
// }
//}
//设置圆角
/*
import UIKit
extension UIImage {
func roundedCornerImageWithCornerRadius(_ cornerRadius:CGFloat) -> UIImage {
let w = self.size.width
let h = self.size.height
var targetCornerRadius = cornerRadius
if cornerRadius < 0 {
targetCornerRadius = 0
}
if cornerRadius > min(w, h) {
targetCornerRadius = min(w,h)
}
let p_w_picpathFrame = CGRect(x: 0, y: 0, width: w, height: h)
UIGraphicsBeginImageContextWithOptions(self.size, false, UIScreen.main.scale)
UIBezierPath(roundedRect: p_w_picpathFrame, cornerRadius: targetCornerRadius).addClip()
self.draw(in: p_w_picpathFrame)
let p_w_picpath = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return p_w_picpath!
}
/*UIImageRenderingModeAutomatic // 根据图片的使用环境和所处的绘图上下文自动调整渲染模式。
UIImageRenderingModeAlwaysOriginal // 始终绘制图片原始状态,不使用Tint Color。
UIImageRenderingModeAlwaysTemplate // 始终根据Tint Color绘制图片,忽略图片的颜色信息。*/
class func p_w_picpathUsedTemplateMode(_ named:String) -> UIImage? {
let p_w_picpath = UIImage(named: named)
if p_w_picpath == nil {
return nil
}
return p_w_picpath!.withRenderingMode(.alwaysTemplate)
}
}
*/
//调用的地方
//
//self.avatarImageView.fin_setImageWithUrl(URL(string: "https:" + avata)!, placeholderImage: nil, p_w_picpathModificationClosure: fin_defaultImageModification() )
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。