我在我的项目中使用AlamofireImage非常多,我使用
let URL = NSURL(string: "https://cdn.domain.com/profile/image.jpg")!
imageView.af_setImageWithURL(URL)
从我的CDN获取图像。我做了一些测试但是如果我错了就纠正我,这似乎将下载的图像存储到缓存中。我的测试包括下载5mb图像。第一次花了大约20秒,第二次是瞬间。
我想知道的是如何清除特定网址/图片的缓存并重新下载图片?
比如说我更新用户个人资料照片。图像名称/ URL将完全相同,但我知道当用户从其库或相机中选择新图像时,图像会发生变化。我知道图像已成功上传到CDN,因为我可以直接在CDN上看到文件夹中的新图像。
答案 0 :(得分:25)
您需要从内存缓存以及磁盘缓存中删除映像。您可以按如下方式执行此操作:
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
。目前尚未在实际版本中提供,但本周某个时候应该在这里。
答案 1 :(得分:3)
希望这可以帮到你:
let URL = NSURL(string: "https://cdn.domain.com/profile/image.jpg")!
let imageDownloader = UIImageView.af_sharedImageDownloader
let imageCache = imageDownloader.imageCache
// Setting CachePolicy as reloadIgnoringLocalCacheData so that it won't use URL Cache next time when it is hitting same URL
let urlRequest = URLRequest(url: URL, cachePolicy: URLRequest.CachePolicy.reloadIgnoringLocalCacheData)
// Clear the Image from the in-memory cache
let _ = imageCache?.removeImage(for: urlRequest, withIdentifier: nil)
imageView.af_setImage(withURLRequest: urlRequest, placeholderImage: UIImage(named: "placeholder"), completion: { (response) in
self.imageView.image = response.result.value
})
答案 2 :(得分:2)
Swift3和AlamofireImage 3.x. 似乎removeImage可以满足所有需要。
// Clear what is in the cache, this will force a refresh to ensure fresh image is loaded next time
let urlRequest = Foundation.URLRequest(url: validUrl)
let imageDownloader = UIImageView.af_sharedImageDownloader
if let imageCache2 = imageDownloader.imageCache {
_ = imageCache2.removeImage(for: urlRequest, withIdentifier: nil)
}
答案 3 :(得分:0)
我的缓存问题解决方案:
func af_setImageIgnoreCache(string: String?) {
guard let url = string, let nsurl = URL(string: url) else { return }
let urlRequest = URLRequest(url: nsurl, cachePolicy: .reloadIgnoringCacheData)
let imageDownloader = ImageDownloader.default
if let imageCache = imageDownloader.imageCache as? AutoPurgingImageCache, let urlCache = imageDownloader.sessionManager.session.configuration.urlCache {
_ = imageCache.removeImages(matching: urlRequest)
urlCache.removeCachedResponse(for: urlRequest)
}
af_setImage(withURLRequest: urlRequest)
}
答案 4 :(得分:0)
@cnoon留下的响应是正确的。它不适用于您中的大多数人的原因是因为您可能正在使用某种过滤器并将nil作为withIdentifier
参数传递。
在这种情况下,我使用了圆形过滤器:
// Clearing the cache didn't work like this
imageDownloader.imageCache?.removeImage(for: urlRequest, withIdentifier: nil)
// Worked when adding a CircleFilter().identifier() as `withIdentifier` as such:
imageDownloader.imageCache?.removeImage(for: urlRequest, withIdentifier: CircleFilter().identifier)
答案 5 :(得分:0)
func clearImageCache(forUrl urlString: String) {
guard let url = URL(string: urlString) else {
return
}
let imageDownloader = UIImageView.af_sharedImageDownloader
let urlRequest = URLRequest(url: url)
// Clear the URLRequest from the in-memory cache
_ = imageDownloader.imageCache?.removeImage(for: urlRequest, withIdentifier: nil)
// Clear the URLRequest from the on-disk cache
imageDownloader.session.sessionConfiguration.urlCache?.removeCachedResponse(for: urlRequest)
}