ZWCommonArrowItem *clearCache = [ZWCommonArrowItem itemWithTitle:@"清除圖片緩存"];
// 1.創(chuàng)建文件管理者
NSFileManager *manager = [NSFileManager defaultManager];
// 獲取緩存文件夾的地址
NSString *sdCachePath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"com.hackemist.SDWebImageCache.default"];
#warning 通過attributesOfItemAtPath獲取文件夾的大小不準(zhǔn)確 ,獲取文件的大小是準(zhǔn)確的
// NSDictionary *attr = [manager attributesOfItemAtPath:sdCachePath error:nil];
// XZLog(@"%@", attr);
// 獲取文件的總大小
long totalsSize = [self fileSizeWithPath:sdCachePath];
// 計(jì)算出文件有多少M(fèi)
CGFloat size = totalsSize / (1024 * 1024.0);
if (size > 0.0) { //如果有緩存顯示緩存文件大小
clearCache.subtitle = [NSString stringWithFormat:@"(%.1fM)", size];
} else {// 沒有緩存不顯示
clearCache.subtitle = nil;
}
// 裝B寫法
// __weak typeof(clearCache) weakClearCache = clearCache;
// __weak typeof(self) weakSelf = self;
__weak ZWCommonArrowItem *weakClearCache = clearCache;
__weak ZWGeneralViewController *weakSelf = self;
clearCache.opertion = ^{
if (weakClearCache.subtitle.length) {
// 1.提示用戶正在清空緩存
[MBProgressHUD showMessage:@"親~正在清除緩存,請(qǐng)不要猴急"];
// 2.真正的清空緩存
// [[SDWebImageManager sharedManager].imageCache clearDisk];
[manager removeItemAtPath:sdCachePath error:nil];
// 隱藏提示框
[MBProgressHUD hideHUD];
// 重新設(shè)置子標(biāo)題
weakClearCache.subtitle = nil;
// 刷新表格
[weakSelf.tableView reloadData];
} else {
[MBProgressHUD showError:@"親~緩存已經(jīng)很干凈了"];
}
};
/**
* 傳入文件路徑返回文件大小
*
* @param filePath 文件路徑
*
* @return 返回文件大小
*/
- (long)fileSizeWithPath:(NSString *)filePath
{
// 1.安全性的校驗(yàn)
// 1.1創(chuàng)建文件管理者
NSFileManager *manager = [NSFileManager defaultManager];
// 1.2通過文件管理者對(duì)象判斷文件是否存在
// 2.判斷是文件還是文件夾
// ExistsAtPath :需要判斷的路徑
// isDirectory :判斷是否是文件夾
// isExists返回值如果是YES代表文件或者文件夾存在如果NO代表不存在
// idr如果返回的是YES代表此文件是文件夾,否則就是文件
BOOL dir = NO;
BOOL isExists = [manager fileExistsAtPath:filePath isDirectory:&dir];
if (isExists == NO) {// 如果返回的是NO代表文件不存在
return 0;// 如果路徑下沒有文件或文件夾直接返回
}
// 2.判斷是文件還是文件夾
if (dir) { // dir是YES代表是此文件是文件夾
// 3.計(jì)算大小
#warning 獲取文件夾下面所有直接子文件的名稱(只能獲取文件夾里的一級(jí)目錄文件如果有文件夾里面還有文件件只能獲取子文件夾的名而不能獲取子文件夾內(nèi)的文件名)
// NSArray *subPaths = [manager contentsOfDirectoryAtPath:filePath error:nil];
#warning 獲取文件平面下面所有(直接和間接)子文件的名稱
NSArray *subPaths = [manager subpathsOfDirectoryAtPath:filePath error:nil];
// XZLog(@"%@", subPaths);
long totalSize = 0;
// 拼接文件的全路徑
for (NSString *subpath in subPaths) {
// 1.拼接文件的全路徑
NSString *fullPath = [filePath stringByAppendingPathComponent:subpath];
// 2.判斷是否是文件夾,如果不是就累加
BOOL isDir = NO;
[manager fileExistsAtPath:fullPath isDirectory:&isDir];
if (isDir == NO) { // 是文件
NSDictionary *attr = [manager attributesOfItemAtPath:fullPath error:nil];
// 累加總文件大小
totalSize += [attr[NSFileSize] longValue];
}
}
return totalSize;
} else { // 是文件
// 計(jì)算大小
NSDictionary *attr = [manager attributesOfItemAtPath:filePath error:nil];
return [attr[NSFileSize] longValue];
}
// 3.計(jì)算大小
return 0;
}
封裝`計(jì)算緩存文件大小并清除緩存`
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
相關(guān)閱讀更多精彩內(nèi)容
- 記錄一下iOS開發(fā)中的設(shè)置界面經(jīng)常用到的清理緩存功能的代碼實(shí)現(xiàn)。 一、流程介紹 緩存文件存到了沙盒...
- 接著上篇下載word/pdf/txt等文檔的文章,此篇涉及到清除這些文檔。本文實(shí)現(xiàn)文件/文件夾大小的計(jì)算以及清除文...
- 移動(dòng)應(yīng)用在處理網(wǎng)絡(luò)資源時(shí),一般都會(huì)做離線緩存處理,其中以圖片緩存最為典型,其中很流行的離線緩存框架為SDWebIm...
- 、、、 、、、清緩存、、、//刪除該文件夾[mgr removeItemAtPath:caches error:n...