import UIKit
import Kingfisher
@H_404_9@
private var lastURLKey: Void?
@H_404_9@
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
,imageModificationClosure:((_ image:UIImage) -> UIImage)? = nil){
self.image = placeholderImage
let resource = ImageResource(downloadURL: URL)
fin_setWebURL(resource.downloadURL)
KingfisherManager.shared.cache.retrieveImage(forKey: resource.cacheKey,options: nil) { (image,cacheType) -> () in
if image != nil {
dispatch_sync_safely_main_queue({ () -> () in
self.image = image
})
}
else {
KingfisherManager.shared.downloader.downloadImage(with: resource.downloadURL,options: nil,progressBlock: nil,completionHandler: { (image,error,imageURL,originalData) -> () in
if let error = error,error.code == KingfisherError.notModified.rawValue {
KingfisherManager.shared.cache.retrieveImage(forKey: resource.cacheKey,completionHandler: { (cacheImage,cacheType) -> () in
self.fin_setImage(cacheImage!,imageURL: imageURL!)
})
return
}
if var image = image,let originalData = originalData {
//处理图片
if let img = imageModificationClosure?(image) {
image = img
}
//保存图片缓存
KingfisherManager.shared.cache.store(image,original: originalData,forKey: resource.cacheKey,toDisk: true,completionHandler: nil)
self.fin_setImage(image,imageURL: imageURL!)
}
})
}
}
}
fileprivate func fin_setImage(_ image:UIImage,imageURL:URL) {
dispatch_sync_safely_main_queue { () -> () in
guard imageURL == self.fin_webURL else {
return
}
self.image = image
}
}
}
@H_404_9@
func fin_defaultImageModification() -> ((_ image:UIImage) -> UIImage) {
return { ( image) -> UIImage in
let roundedImage = image.roundedCornerImageWithCornerRadius(3)
return roundedImage
}
}
@H_404_9@
@H_404_9@
//func dispatch_sync_safely_main_queue(_ block: ()->()) {
// if Thread.isMainThread {
// block()
// } else {
// DispatchQueue.main.sync {
// block()
// }
// }
//}
@H_404_9@
@H_404_9@
//设置圆角
@H_404_9@
/*
import UIKit
@H_404_9@
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 imageFrame = CGRect(x: 0,y: 0,width: w,height: h)
UIGraphicsBeginImageContextWithOptions(self.size,false,UIScreen.main.scale)
UIBezierPath(roundedRect: imageFrame,cornerRadius: targetCornerRadius).addClip()
self.draw(in: imageFrame)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image!
}
/*UIImageRenderingModeAutomatic // 根据图片的使用环境和所处的绘图上下文自动调整渲染模式。
UIImageRenderingModeAlwaysOriginal // 始终绘制图片原始状态,不使用Tint Color。
UIImageRenderingModeAlwaysTemplate // 始终根据Tint Color绘制图片,忽略图片的颜色信息。*/
class func imageUsedTemplateMode(_ named:String) -> UIImage? {
let image = UIImage(named: named)
if image == nil {
return nil
return image!.withRenderingMode(.alwaysTemplate)
}
*/
@H_404_9@
@H_404_9@
//调用的地方
//
//self.avatarImageView.fin_setImageWithUrl(URL(string: "https:" + avata)!,placeholderImage: nil,imageModificationClosure: fin_defaultImageModification() )
原文链接:https://www.f2er.com/swift/321770.html