-(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;
}