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?

Eugene
  • 10,006
  • 4
  • 37
  • 55
  • For all the people that just want to re-download some images every time, pass `SDWebImageRefreshCached` into the options – Eugene Feb 10 '14 at 11:18

5 Answers5

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

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

Community
  • 1
  • 1
Carmen
  • 6,177
  • 1
  • 35
  • 40
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.

Eugene
  • 10,006
  • 4
  • 37
  • 55
  • not able to call clearcache method. [[SDWebImageManager sharedManager] clearCaches] . i tried this but it is crashing. please help me. – Suraj Shinde May 31 '13 at 15:33
  • 3
    @SurajShinde There's no such method for `SDImageCache`, of course it will crash. Use `[[[SDWebImageManager sharedManager] imageCache] clearDisk];`, `[[[SDWebImageManager sharedManager] imageCache] clearMemory];` – Eugene May 31 '13 at 18:24
  • Thanks, Actully i was using old SDwebcache it did not have imageCache instance. – Suraj Shinde Jun 01 '13 at 19:11
  • 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: https://github.com/rs/SDWebImage/blob/master/SDWebImage/SDWebImageManager.m – Artem M Feb 16 '16 at 08:04
2

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)
The iCoder
  • 1,414
  • 3
  • 19
  • 39
-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.

Göktuğ Aral
  • 1,409
  • 1
  • 13
  • 28
-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.

Bomi Chen
  • 81
  • 4