您好,登錄后才能下訂單哦!
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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。