Eugene
Eugene

Reputation: 10045

SDWebImage clearing cache

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?

Upvotes: 36

Views: 34933

Answers (5)

Bomi Chen
Bomi Chen

Reputation: 122

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.

Upvotes: -2

The iCoder
The iCoder

Reputation: 1444

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)

Upvotes: 4

Göktuğ Aral
Göktuğ Aral

Reputation: 1409

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.

Upvotes: -2

Carmen
Carmen

Reputation: 6263

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

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

Upvotes: 114

Eugene
Eugene

Reputation: 10045

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.

Upvotes: 10

Related Questions