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
Zip and Unzip a File Programmatically in iOS
How to Add Watermark on a Exist Image
Autorelease Pools and When Release Is Called Under iOS
Setneedsdisplayinrect: Causes the Whole View to Be Updated
Is It Just the iPhone Simulator That Is Restricted to Intel Only MAC'S
Core Location Not Working in iOS 8
Playing Video into Uitableview
Passing Data with Segue Through Navigationcontroller
iPhone - Didselectrowatindexpath: Only Being Called After Long Press on Custom Cell
Could Not Attach to Pid:"####" Unable to Attach
Opening Word,Excel, and PDF Files Without Using Uiwebview on iOS
Difference Between Keychain and Nsuserdefault
Uitextfield Clearbuttonmode Color
New Itunes User Interface Unable to Find "Ready to Upload Binary"