我经常在我的项目中使用AlamofireImage,而且我使用
let URL = NSURL(string: "https://cdn.domain.com/profile/image.jpg")!
imageView.af_setImageWithURL(URL)从我的CDN中获取图像。我已经做了一些测试,但纠正我如果我是错误的,这似乎是存储在缓存下载的图像。我的测试包括下载一个5mb的映像。第一次花了大约20秒,第二次是即时的。
我想知道的是,如何清除特定URL/图像的缓存并重新下载图像?
比如说,我更新了一个用户配置文件图片。图像名称/URL将完全相同,但我知道图像有变化,因为用户从他们的库或照相机中选择了一个新的图像。我知道图像已经成功地上传到CDN,因为我可以看到新的图像在文件夹中直接在CDN上。
发布于 2015-11-02 15:35:47
您需要从内存缓存和磁盘缓存中删除图像。您可以这样做:
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,它允许访问ImageDownloader中的底层sessionManager。这在实际发行版中还没有提供,但应该在本周某个时候出现。
发布于 2016-11-16 23:24:24
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)
}发布于 2017-12-05 09:57:17
希望这能帮到你:
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
})https://stackoverflow.com/questions/33467263
复制相似问题