// 在子线程计算缓存大小
dispatch_async(dispatch_get_global_queue(0, 0), ^{// 获得缓存文件夹路径
unsigned long long size = file.fileSize;
size += [SDImageCache sharedImageCache].getSize;
NSString *sizeText = nil;
if (size >= pow(10, 9)) { // size >= 1GB
sizeText = [NSString stringWithFormat:@"%.2fGB", size / pow(10, 9)];
} else if (size >= pow(10, 6)) { // 1GB > size >= 1MB
sizeText = [NSString stringWithFormat:@"%.2fMB", size / pow(10, 6)];
} else if (size >= pow(10, 3)) { // 1MB > size >= 1KB
sizeText = [NSString stringWithFormat:@"%.2fKB", size / pow(10, 3)];
} else { // 1KB > size
sizeText = [NSString stringWithFormat:@"%zdB", size];
}
// 生成文字
NSString *text = [NSString stringWithFormat:@"清除缓存(%@)", sizeText];
// 回到主线程
dispatch_async(dispatch_get_main_queue(), ^{
// 设置文字
// 恢复点击事件
self.userInteractionEnabled = YES;
});
});
}
return self;
}
/**
* 清除缓存
*/
- (void)clearCache
{
// 弹出指示器
[SVProgressHUD showWithStatus:@"正在清除缓存..." maskType:SVProgressHUDMaskTypeBlack];
// 删除SDWebImage的缓存
[[SDImageCache sharedImageCache] clearDiskOnCompletion:^{
// 删除自定义的缓存
dispatch_async(dispatch_get_global_queue(0, 0), ^{
NSFileManager *mgr = [NSFileManager defaultManager];
[mgr removeItemAtPath:file error:nil];
[mgr createDirectoryAtPath:XMGCustomCacheFile withIntermediateDirectories:YES attributes:nil error:nil];
// 所有的缓存都清除完毕
dispatch_async(dispatch_get_main_queue(), ^{
// 隐藏指示器
[SVProgressHUD dismiss];
// 设置文字
self.textLabel.text = @"清除缓存(0B)";
});
});
}];
}