SDWebImage clearing cache
Asked Answered
J

5

36

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.

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.

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

Jasun answered 13/12, 2012 at 17:50 Comment(1)
For all the people that just want to re-download some images every time, pass SDWebImageRefreshCached into the optionsJasun
D
114
SDImageCache *imageCache = [SDImageCache sharedImageCache];
[imageCache clearMemory];
[imageCache clearDisk];

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

Dobbin answered 29/4, 2013 at 11:23 Comment(5)
The current version of SDWebImage will automatically clear the memory cache when memory warnings occur. No need to do this and especially no need to clear disk cache.Sheers
can you please add a source. Which version will support these functionality exactly?Dobbin
better to use the non-blocking version - clearDiskOnCompletion:Lactoflavin
@Rizon, clearDisk is not blocking! The only difference is that it doesn't have a completion callback.Myrmecophagous
Update for the latest release: SDImageCache *imageCache = [SDImageCache sharedImageCache]; [imageCache clearMemory]; [imageCache clearDiskOnCompletion:^(){}];Lamphere
J
10

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 SDWebImageManager which 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.

Jasun answered 14/12, 2012 at 15:31 Comment(4)
not able to call clearcache method. [[SDWebImageManager sharedManager] clearCaches] . i tried this but it is crashing. please help me.Circumgyration
@SurajShinde There's no such method for SDImageCache, of course it will crash. Use [[[SDWebImageManager sharedManager] imageCache] clearDisk];, [[[SDWebImageManager sharedManager] imageCache] clearMemory];Jasun
Thanks, Actully i was using old SDwebcache it did not have imageCache instance.Circumgyration
No need for this, the marked answer is doing the same thing. imageCache instance variable of SDWebImageManager just returns [SDImageCache sharedImageCache]. Look at the createCache method: github.com/rs/SDWebImage/blob/master/SDWebImage/…Tubule
C
3

Only following code worked for me : Swift 5.0, Xcode 11, iOS 13, SDWebImage pod 5.0

 SDWebImageManager.shared.imageCache.clear(with: .all) {
        print("deleted all")
 }

where you choose options like SDImageCacheType.disk, SDImageCacheType.memory, SDImageCacheType.disk

Also if you want to remove specific image from cache use following:

SDWebImageManager.shared.imageCache.removeImage(forKey: "url of image", cacheType: .all)
Corny answered 10/6, 2020 at 10:54 Comment(2)
This also works with Swift 4 but: SDWebImageManager.shared()?.imageCache.removeImage(forKey: "url of image")Nano
In SDWebImage 5.x show use SDImageCache.shared.clearMemory() insteadKatrinka
F
-2

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.

Main issue was url ampersand encoding!

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.

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

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

Also the latest versions of SDWebImage library has include UIImageView+WebCache.h class and it really nicely handle cache problems.

Fuse answered 12/5, 2015 at 10:22 Comment(0)
S
-2

The icons can be changed on server side,

so you need to load with refresh cached every time.

if you're using latest SDWebImage framework (5.12.x)

you can call it like this,

[imgView sd_setImageWithURL:[NSURL URLWithString:urlStr] placeholderImage:[UIImage imageNamed:@"xxx" options:SDWebImageRefreshCached];

edit at 220112.

Snap answered 20/10, 2021 at 8:21 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.