How to clear all cached images loaded from SDWebImage?
Asked Answered
E

5

31

I have all images loaded on my app via SDWebImage. The downloading and caching works great, but I wanted to make a button that can clear all cached images in the entire app.

I have a "Clear Cache" button as a UIButton on one of my tab bar views. How can I make it so when this button is tapped, all the cached images are removed and need to be re-downloaded?

Using Swift.

Thank you!

Exalt answered 5/10, 2016 at 18:48 Comment(0)
E
68

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()
}
Endocrine answered 5/10, 2016 at 18:57 Comment(5)
Thanks for the response. How would I link this up to a UIButton? Would it be through an outlet?Exalt
Will try this. Thank you!Exalt
Is SDWebimage library manage automatic remove images from the cached? If we are not using any more since 2 to 3 days and more.Catherin
FYI, clearDisk has been changed to clearDiskOnCompletion:.Fitzhugh
@Fitzhugh the declaration is func clearDisk(onCompletion completion: SDWebImageNoParamsBlock? = nil), so the onCompletion parameter can be omitted.Snow
L
2

Try this:

@IBAction func actClearCache(sender:AnyObject) {

   let objCache = SDImageCache.sharedImageCache()
   objCache.clearMemory()
   objCache.cleanDisk()

}
Labialized answered 5/10, 2016 at 18:56 Comment(7)
Thanks for the response. How can I link this up to a UIButton? Would it be through an outlet?Exalt
Got it, thanks! I am getting an error that says: Use of unresolved identifier 'SDImageCache'. Would I need to import it in my bridging header?Exalt
Have you ‘import SDWebImage`? And which swift version are you using?Labialized
Here's my bridging header: #import "SDWebImage/UIImageView+WebCache.h" #import "SDWebImane" It says that 'SdWebImane' file not foundExalt
I imported SDWebImage, and I think I'm using Swift 2.2Exalt
I didn't write "import SDWebImage" on the .swift file attached to the View controller. It works now that I've wrote it!Exalt
As of this date cleanDisk and clearDisk are missing from SDImageCache.h. But they're still in cocoadocs.org/docsets/SDWebImage/3.3/Classes/SDImageCache.html#/…Probation
B
2

To delete all data from cache

SDImageCache.shared.clear(with: .all) {
    print("Disk & memory data cleared")
}

To delete data from only memory

SDImageCache.shared.clear(with: .memory) {
    print("Memory data cleared")
}

To delete data from disk

SDImageCache.shared.clear(with: .disk) {
    print("Disk data cleared")
}
Bacteriology answered 12/1, 2021 at 1:6 Comment(0)
R
1

Swift 4.2 , Xcode 10

pod 'SDWebImage', '5.0.0-beta3'

import SDWebImage

 @IBAction func ClearCacheButtonClick(_ sender: UIButton) {
     SDImageCache.shared.clearMemory()
     SDImageCache.shared.clearDisk()
 }
Ruhl answered 23/11, 2018 at 10:12 Comment(0)
C
0

Swift 5

import SDWebImage

  SDImageCache.shared.clearMemory()

  SDImageCache.shared.clearDisk()
Calif answered 25/5, 2020 at 6:50 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.