利用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];//清除內(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;

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容