如何清除AlamofireImage setImageWithURL缓存 [英] How to clear AlamofireImage setImageWithURL cache
问题描述
我在项目中经常使用AlamofireImage,并且使用
I am using AlamofireImage in my project quite a lot and I use
let URL = NSURL(string: "https://cdn.domain.com/profile/image.jpg")!
imageView.af_setImageWithURL(URL)
从我的CDN中获取图像。我已经进行了一些测试,但是如果我错了,请更正我,这似乎会将下载的图像存储到缓存中。我的测试包括下载5mb的图像。第一次大约花了20秒,第二次是瞬间。
to fetch an image from my CDN. I have done some tests but correct me if I am wrong, this seems to store the downloaded image in to a cache. My tests included downloading a 5mb image. The first time it took about 20 seconds, the second time was instant.
我想知道的是如何清除特定URL /图像的缓存并重新下载图片?
The thing I would like to know is how can I clear the cache for a specific URL/image and re-download the image?
例如,我更新了用户个人资料照片。图像名称/ URL会完全相同,但是我知道图像会随着用户从库或相机中选择新图像而发生变化。我知道该图像已成功上传到CDN,因为我可以直接在CDN上的文件夹中看到新图像。
Say for example I update a users profile pic. The image name/URL will be exactly the same but I know the image has changes as the user selected a new image from their library or camera. I know the image has been uploaded successfully to the CDN as I can see the new image in the folder directly on the CDN.
推荐答案
您需要从内存缓存和磁盘缓存中删除映像。您可以执行以下操作:
You need to remove the image from the in-memory cache as well as the on-disk cache. You can do this as follows:
func clearImageFromCache() {
let URL = NSURL(string: "https://cdn.domain.com/profile/image.jpg")!
let URLRequest = NSURLRequest(URL: URL)
let imageDownloader = UIImageView.af_sharedImageDownloader
// Clear the URLRequest from the in-memory cache
imageDownloader.imageCache?.removeImageForRequest(URLRequest, withAdditionalIdentifier: nil)
// Clear the URLRequest from the on-disk cache
imageDownloader.sessionManager.session.configuration.URLCache?.removeCachedResponseForRequest(URLRequest)
}
当前, URLCache
只能以这种方式在 master
分支上被清除。我刚刚推送了 f35e4748 ,它允许访问底层的 sessionManager
ImageDownloader
中的code>。尚未在实际版本中提供此功能,但应该在本周的某个时间出现。
Currently, the URLCache
can only be cleared in this manner on the master
branch. I just pushed f35e4748 which allows access to the underlying sessionManager
in the ImageDownloader
. This is not yet available in an actual release yet, but should be here sometime this week.
这篇关于如何清除AlamofireImage setImageWithURL缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!