UIImageView+Extension.swift,Kingfisher

前端之家收集整理的这篇文章主要介绍了UIImageView+Extension.swift,Kingfisher前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

,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

}

}

}


func fin_defaultImageModification() -> ((_ image:UIImage) -> UIImage) {

return { ( image) -> UIImage in

let roundedImage = image.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 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)

}

*/



//调用的地方

//

//self.avatarImageView.fin_setImageWithUrl(URL(string: "https:" + avata)!,placeholderImage: nil,imageModificationClosure: fin_defaultImageModification() )

猜你在找的Swift相关文章