如何清除AlamofireImage setImageWithURL缓存

编程入门 行业动态 更新时间:2024-10-13 06:13:59
本文介绍了如何清除AlamofireImage setImageWithURL缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在项目中经常使用AlamofireImage,并且使用

I am using AlamofireImage in my project quite a lot and I use

let URL = NSURL(string: "cdn.domain/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: "cdn.domain/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 。尚未在实际版本中提供此功能,但应该在本周的某个时间出现。

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缓存

本文发布于:2023-11-26 08:39:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1633406.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:缓存   AlamofireImage   setImageWithURL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!