import UIKit
定远网站制作公司哪家好,找成都创新互联公司!从网页设计、网站建设、微信开发、APP开发、响应式网站开发等网站项目制作,到程序开发,运营维护。成都创新互联公司2013年至今到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选成都创新互联公司。
import Kingfisher
private var lastURLKey: Void?
extensionUIImageView {
public var fin_webURL: URL? {
returnobjc_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() )