ios SDWebImage清除缓存

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13865537/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:33:00  来源:igfitidea点击:

SDWebImage clearing cache

iphoneobjective-ciossdwebimage

提问by Eugene

I'm displaying a list of icons downloaded from the web with text in a table view. The icons can be changed on server side and I need to replace them as soon as new icons are getting available. I try using the following code:

我正在显示从网络下载的图标列表,并在表格视图中显示文本。图标可以在服务器端更改,我需要在新图标可用时立即替换它们。我尝试使用以下代码:

[imgView setImageWithURL:url placeholderImage:[UIImage imageNamed:@"table_avatar_icon"] options:SDWebImageCacheMemoryOnly];

And call [[SDImageCache sharedImageCache] clearMemory];In my refresh callback, but it does not purge the contents of the cache. More to it, even if I close the application and open it again the image is still there.

[[SDImageCache sharedImageCache] clearMemory];在我的刷新回调中调用,但它不会清除缓存的内容。更重要的是,即使我关闭应用程序并再次打开它,图像仍然存在。

I found only one way to clear the cache and it is by calling [[SDImageCache sharedImageCache] clearDisk];. Which only works after I close and reopen the app.

我只找到了一种清除缓存的方法,它是通过调用[[SDImageCache sharedImageCache] clearDisk];. 这仅在我关闭并重新打开应用程序后才有效。

How can I force SDWebImage to not to use disk caching?

如何强制 SDWebImage 不使用磁盘缓存?

回答by carmen_munich

SDImageCache *imageCache = [SDImageCache sharedImageCache];
[imageCache clearMemory];
[imageCache clearDisk];

Don't forget to put these lines of code in your didReceiveMemoryWarning, too.

不要忘记将这些代码行也放入您的didReceiveMemoryWarning.

回答by Eugene

Located the source if an issue. It seems that I was deceived by the Singleton pattern used in SDImageCache. The cache for extension that is used over UIImageView is being controlled by SDWebImageManagerwhich has an instance variable of SDImageCache. If you want to clear the cache for extension you have to call its imageCache's methods like clearDisk and clearMemory.

如果出现问题,请找到源。看来我是被SDImageCache. 在 UIImageView 上使用的扩展缓存由SDWebImageManager它的实例变量SDImageCache. 如果你想清除扩展的缓存,你必须调用它imageCache的方法,比如 clearDisk 和 clearMemory。

回答by G?ktu? Aral

Except SDImageCache methods, I strongly advise you to check your image urls.In my situation I tried every method for imageCache and memory issue was still continue. Crashes were occur mainly on iPhone 4s because of hardware it couldn't handle it.

除了 SDImageCache 方法,我强烈建议您检查您的图片网址。在我的情况下,我尝试了 imageCache 的所有方法,但内存问题仍在继续。崩溃主要发生在 iPhone 4s 上,因为它无法处理硬件。

Main issue was url ampersand encoding!

主要问题是url & 编码!

In example, check out these urls: first url is using "&amp" and second one is not. Because of ampersand my JSON library can't read the width and width value get much higher then it should be. That is why I had a memory issue.

例如,查看这些 url:第一个 url 使用“&”,第二个不使用。由于 & 我的 JSON 库无法读取宽度和宽度值变得比它应该的高得多。这就是为什么我有记忆问题。

1) /select.php?imageid=101961221 "&amp";width=100 "&amp";isWatermarked=true

1) /select.php?imageid=101961221 "&";width=100 "&";isWatermarked=true

2) /select.phpimageid=101961221&width=100&isWatermarked=true

2) /select.phpimageid=101961221&width=100&isWatermarked=true

Also the latest versions of SDWebImage libraryhas include UIImageView+WebCache.hclass and it really nicely handle cache problems.

另外,SDWebImage库的最新版本已经包括UIImageView+WebCache.h类,它真的很好地处理了缓存的问题。