2017-09-13 76 views
1

我需要SDWebImageManager的完成处理函数(设置下载或缓存的图像黑白),所以我使用它而不是sd_setimage。我的问题是,我不知道如何使用SDWebImageManager下载或获取图像的缓存版本。每次tableview单元出队和重新加载时,图像都会重新下载。我尝试设置选项:SDWebImageDownloaderOptions.useNSURLCache,但无济于事。任何建议将不胜感激!这里是我的代码:如何使用SDWebImageManager下载图片只有在尚未缓存的情况下才能下载图片?

SDWebImageManager.shared().imageDownloader?.downloadImage(with:URL(string: imgURL), options: SDWebImageDownloaderOptions.useNSURLCache, progress: nil, completed: { (image, error, cacheType, url) in 
    if image != nil { 
     let beginImage = CIImage(image: image!) 
     let blackNwhiteImg = beginImage?.applyingFilter("CIColorControls", withInputParameters: [kCIInputSaturationKey:0.0]) 
     let newImage = UIImage(ciImage: blackNwhiteImg!) 
     cell.button.setImage(newImage, for: .normal) 
    } 
}) 

回答

1

从缓存中拉更新答案:

SDWebImageManager.shared().loadImage(with: URL?, options: SDWebImageOptions, progress: { (Int, Int, URL?) in 
    code 
}, completed: { (UIImage?, Data?, Error?, SDImageCacheType, Bool, URL?) in 
    code 
}) 

以供参考,我包括这张截图,显示了评论的XCode礼物虽然进入功能:

enter image description here

另外,包含在SDWebImageManager文件中的注释:

/** 
* Downloads the image at the given URL if not present in cache or return the cached version otherwise. 
* 
* @param url   The URL to the image 
* @param options  A mask to specify options to use for this request 
* @param progressBlock A block called while image is downloading 
*      @note the progress block is executed on a background queue 
* @param completedBlock A block called when operation has been completed. 
* 
* This parameter is required. 
* 
* This block has no return value and takes the requested UIImage as first parameter and the NSData representation as second parameter. 
* In case of error the image parameter is nil and the third parameter may contain an NSError. 
* 
* The forth parameter is an `SDImageCacheType` enum indicating if the image was retrieved from the local cache 
* or from the memory cache or from the network. 
* 
* The fith parameter is set to NO when the SDWebImageProgressiveDownload option is used and the image is 
* downloading. This block is thus called repeatedly with a partial image. When image is fully downloaded, the 
* block is called a last time with the full image and the last parameter set to YES. 
* 
* The last parameter is the original image URL 
* 
* @return Returns an NSObject conforming to SDWebImageOperation. Should be an instance of SDWebImageDownloaderOperation 
+0

非常感谢您的回复。这与我发布的代码完全相同。在我看来,你的代码和我的代码都在访问相同的.downloadImage()函数。你能用语法更具体吗?我没有设置选项吗?我尝试使用和不使用选项:SDWebImageDownloaderOptions.useNSURLCache。再次感谢。 –

+0

我的情况的另一个细节是,我注意到在出队tableview单元格中的图像重新加载。但根据我对sdwebimage的理解,出队和重新加载的单元应该无缝地重新加载缓存的图像。 –

+0

每次加载图像时,我都会在完成块中打印cacheType值。每次都是零。这似乎表明没有图像缓存? –

1

您可以使用SDWebImageManager.shared().cachedImageExists(for: imageUrl)知道如果URL缓存

之后,你可以得到cacheKey用这种方法

let cacheKey = SDWebImageManager.shared().cacheKey(for: imageUrl) 

具有CacheKey可以调用此方法,并在他的封你会得到你的缓存图像

SDWebImageManager.shared().imageCache.queryDiskCache(forKey: cacheKey, done: { (image, cacheType) in 

       }) 
+0

感谢您的回复。然后,如何在完成块中获取该URL的缓存图像?据我所知,没有和“图像”值传入。 –

+0

@DanielGrigsby检查我更新的答案 –

+0

感谢您的回答。了解CacheKey非常有用。我认为.loadImage是我正在寻找的。 –