How to clear all cached images loaded from SDWebIm

2020-02-09 07:00发布

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!

3条回答
爱情/是我丢掉的垃圾
2楼-- · 2020-02-09 07:12

Try this:

@IBAction func actClearCache(sender:AnyObject) {

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

}
查看更多
聊天终结者
3楼-- · 2020-02-09 07:23

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

Obj-c:

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

Swift:

import SDWebImage

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

Swift 3.0

import SDWebImage

@IBAction func clearCache(sender: UIButton) {
    SDImageCache.shared().clearMemory()
    SDImageCache.shared().clearDisk()
}
查看更多
成全新的幸福
4楼-- · 2020-02-09 07:37

Swift 4.2 , Xcode 10

pod 'SDWebImage', '5.0.0-beta3'

import SDWebImage

 @IBAction func ClearCacheButtonClick(_ sender: UIButton) {
     SDImageCache.shared.clearMemory()
     SDImageCache.shared.clearDisk()
 }
查看更多
登录 后发表回答