-(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];//清除內(nèi)存緩存
[[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字節(jié)
CGFloat totalSize = (sdSize+size)/1024.0/1024.0;
return totalSize;
}