如何清除AlamofireImage setImageWithURL缓存

puk*_*978 11 swift alamofire

我在我的项目中使用了AlamofireImage并且使用了很多

let URL = NSURL(string: "https://cdn.domain.com/profile/image.jpg")!
imageView.af_setImageWithURL(URL)
Run Code Online (Sandbox Code Playgroud)

从我的CDN获取图像.我做了一些测试但是如果我错了就纠正我,这似乎将下载的图像存储到缓存中.我的测试包括下载5mb图像.第一次花了大约20秒,第二次是瞬间.

我想知道的是如何清除特定URL /图像的缓存并重新下载图像?

比如说我更新用户个人资料照片.图像名称/ URL将完全相同,但我知道当用户从其库或相机中选择新图像时,图像会发生变化.我知道图像已成功上传到CDN,因为我可以直接在CDN上看到文件夹中的新图像.

cno*_*oon 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)
}
Run Code Online (Sandbox Code Playgroud)

目前,URLCache只能在master分支上以这种方式清除.我刚推了f35e4748,允许访问底层sessionManagerImageDownloader.这还没有在实际版本中提供,但应该在本周的某个时间.

  • 出于某种原因,这对我不起作用.即使远程图像已经改变,我仍然可以获得相同的图像. (5认同)