[英]SDWebImage process images before caching
我從網上獲取了很多圖像,它們都是各種尺寸 - 它們可以是大的,小的等等。
因此,當我在單元格中顯示它們時,我可以調整它們的大小,但這是低效的。 在SDWebImage下載它們並緩存它們的大小后,重新調整它們的方式會更好,而不是將大圖像存儲在磁盤上並為每個單元調整大小。
那么我怎么能用SDWebImage做到這一點,或者我必須在課堂上進行一些修改?
我和你有同樣的問題,並嘗試首先調整SDWebImage,但最終構建了我自己的組件來解決問題。 你可以在這里看看它: https : //github.com/adig/RemoteImageView
SDWebImage 3.8.2
如果使用UIImageView類別sd_setImageWithURL
。 我創建了另一個UIImageView類別(擴展名)
func modifiedImageFromUrl(url: NSURL?) {
self.sd_setImageWithURL(url) { (image, error, cacheType, url) in
if cacheType == SDImageCacheType.None && image != nil {
dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0)) {
let modifiedImage = // modify image as you want
dispatch_async(dispatch_get_main_queue()) {
SDWebImageManager.sharedManager().saveImageToCache(modifiedImage, forURL: url)
self.image = modifiedImage
}
}
}
}
}
擴展MaeSTRo在Swift 3中的答案:
myImageView.sd_setImage(with: imageUrl){ (image, error, cacheType, url) in
guard let image = image, cacheType == .none else { return }
DispatchQueue.global(qos: .userInitiated).async {
let modifiedImage = myImageProcessor(image)
SDWebImageManager.shared().saveImage(toCache: modifiedImage, for: imageUrl)
DispatchQueue.main.async {
myImageView.image = modifiedImage
myImageView.setNeedsDisplay()
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.