利用SDWenImage清除缓存

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

NSString *sizeStr = [NSString stringWithFormat:@"%.2fM",[self getCacheSize]];

if (indexPath.row == 0) {

UIAlertController *actionsheet=[UIAlertController alertControllerWithTitle:@"清除缓存" message:sizeStr preferredStyle:UIAlertControllerStyleAlert ];

[actionsheet   addAction:[UIAlertAction actionWithTitle:@"清除" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {

//1.删除sd

[[SDImageCache sharedImageCache]clearMemory];//清除内存缓存

[[SDImageCache sharedImageCache]clearDisk];//清除磁盘

//2.界面下载的缓存

NSString *myPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches/MyCaches"];

//删除

[[NSFileManager defaultManager]removeItemAtPath:myPath error:nil];

}]];

[actionsheet addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {

NSLog(@"取消");

}]];

[self presentViewController:actionsheet animated:YES completion:nil];

}

//获取缓存大小

-(CGFloat)getCacheSize{

//缓存有两类 sdwebimage还有每个界面保存的缓存

CGFloat sdSize = [[SDImageCache sharedImageCache]getSize];

NSString *myPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches/MyCaches"];

//获取文件夹中的所有文件

NSArray *arr = [[NSFileManager defaultManager]contentsOfDirectoryAtPath:myPath error:nil];

unsigned long long size = 0;

for (NSString *fileName in arr) {

NSString *filePath = [myPath stringByAppendingPathComponent:fileName];

NSDictionary *dict = [[NSFileManager defaultManager ]attributesOfItemAtPath:filePath error:nil];

size += dict.fileSize;

}

//1M = 1024 K = 1024*1024字节

CGFloat totalSize = (sdSize+size)/1024.0/1024.0;

return totalSize;

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容