How to Clear All Cached Images Loaded from Sdwebimage

How to clear all cached images loaded from SDWebImage?

If you want to completely clear the cache do the following:

Obj-c:

- (IBAction)clearCache:(id)sender {
[[SDImageCache sharedImageCache]clearMemory];
[[SDImageCache sharedImageCache]clearDisk];
}

Swift 5

SDImageCache.shared.clearMemory()
SDImageCache.shared.clearDisk()

Swift 3.0

@IBAction func clearCache(sender: UIButton) {
SDImageCache.shared().clearMemory()
SDImageCache.shared().clearDisk()
}

How to delete cached SDWebImage from a button's imageView

To remove the image from UIButton you need to mention the state as well.

self.profileButton.setImage(nil, for: .normal)

SDWebImage clearing cache

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

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

Remove image cache from SDWebImage

The method - (void)removeImageForKey:(NSString *)key fromDisk:(BOOL)fromDisk in the SDWebImage takes NSString as an argument.

In your above code it looks like [self.imgArray objectAtIndex:indexForDelete] gives you an an NSDictionary object and hence the crash.

So you would need something like this;

NSDictionary *imageDictionary = [self.imgArray objectAtIndex:indexForDelete];
NSString *cacheKeyToDelete = [imageDictionary objectForKey:@"YourKeyForImageCacheKey"];

[[SDImageCache sharedImageCache] removeImageForKey:cacheKeyToDelete fromDisk:YES];

SDWebImage - avoid clearing image cache on entering background

Go to SDImageCache.m and seek method - (id)initWithNamespace:(NSString *)ns
There you will find such code:

// Subscribe to app events
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(clearMemory)
name:UIApplicationDidReceiveMemoryWarningNotification
object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(cleanDisk)
name:UIApplicationWillTerminateNotification
object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(backgroundCleanDisk)
name:UIApplicationDidEnterBackgroundNotification
object:nil];

Feel free to comment out or change anything if you want. I'd make an option to turn off these lines of code.

Clearing cache in SDWebImage for iOS app

Just following up in case anyone finds this searching. To correct the problem, what you want to do is make sure the full SDWebImage package is moved into your framework folder.

At that point, you can find a WebImageCache.m file and tailor it to your specific cache needs.

Additionally, if you look in the same project on the Github, you can actually install a "clear cache" bar that uses the SDWebImageManager to enable the user to clear the cache. In my particular instance, that was very useful.

Good luck!



Related Topics



Leave a reply



Submit