SDWebImage清除图片缓存

时间:2023-03-09 22:45:25
SDWebImage清除图片缓存

背景: 使用 SDWebImage 库,由于内存中一直缓存着加载的图片,而导致内存过高(我们无法手动管理内存),弹出内存警告而导致程序很卡或者直接crash掉。

我的解决方法:

在AppDelegate.m文件中,引入SDWebImageManager.h 文件,然后使用它的内存警告方法,调用清除缓存方法;(注意这是清除内存中的缓存)

- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application
{
// 停止下载图片
[[SDWebImageManager sharedManager] cancelAll]; // 清除内存缓存图片
[[SDWebImageManager sharedManager].imageCache clearMemory]; }

    还有一种方法:(调用下面这个方法)

- (void)clearTmpPics
{
[[SDImageCache sharedImageCache] clearDisk]; //清除硬盘 [[SDImageCache sharedImageCache] clearMemory];//清除内存 DLog(@"clear disk"); float tmpSize = [[SDImageCache sharedImageCache] checkTmpSize]; NSString *clearCacheName = tmpSize >= 1 ? [NSString stringWithFormat:@"清理缓存(%.2fM)",tmpSize] : [NSString stringWithFormat:@"清理缓存(%.2fK)",tmpSize * 1024]; [configDataArray replaceObjectAtIndex:2 withObject:clearCacheName]; [configTableView reloadData];
}

  以上~