手機(jī)上app的實(shí)際文件夾路徑還是蠻多的,有些應(yīng)該是不提倡刪除的,不過(guò)像是一些緩存什么的幌缝,清理一下還是必要的,用到的一些第三方類庫(kù)如果存在自動(dòng)緩存的诫欠,也會(huì)提供相應(yīng)的清理和計(jì)算方法涵卵。
下面就直接封裝一下吧:
.h文件
.h文件封裝兩個(gè)方法出來(lái)
/*s*
* 獲取path路徑下文件夾的大小
*
* @param path 要獲取的文件夾 路徑
*
* @return 返回path路徑下文件夾的大小
*/
+ (NSString *)getCacheSizeWithFilePath:(NSString *)path;
/**
* 清除path路徑下文件夾的緩存
*
* @param path 要清除緩存的文件夾 路徑
*
* @return 是否清除成功
*/
+ (BOOL)clearCacheWithFilePath:(NSString *)path;
.m文件
.m文件來(lái)實(shí)現(xiàn)具體方法,老規(guī)矩還是用什么寫什么
#pragma mark - 獲取path路徑下文件夾大小
+ (NSString *)getCacheSizeWithFilePath:(NSString *)path{
// 獲取“path”文件夾下的所有文件
NSArray *subPathArr = [[NSFileManager defaultManager] subpathsAtPath:path];
NSString *filePath = nil;
NSInteger totleSize = 0;
for (NSString *subPath in subPathArr){
// 1. 拼接每一個(gè)文件的全路徑
filePath =[path stringByAppendingPathComponent:subPath];
// 2. 是否是文件夾荒叼,默認(rèn)不是
BOOL isDirectory = NO;
// 3. 判斷文件是否存在
BOOL isExist = [[NSFileManager defaultManager] fileExistsAtPath:filePath isDirectory:&isDirectory];
// 4. 以上判斷目的是忽略不需要計(jì)算的文件
if (!isExist || isDirectory || [filePath containsString:@".DS"]){
// 過(guò)濾: 1. 文件夾不存在 2. 過(guò)濾文件夾 3. 隱藏文件
continue;
}
// 5. 指定路徑轿偎,獲取這個(gè)路徑的屬性
NSDictionary *dict = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil];
/**
attributesOfItemAtPath: 文件夾路徑
該方法只能獲取文件的屬性, 無(wú)法獲取文件夾屬性, 所以也是需要遍歷文件夾的每一個(gè)文件的原因
*/
// 6. 獲取每一個(gè)文件的大小
NSInteger size = [dict[@"NSFileSize"] integerValue];
// 7. 計(jì)算總大小
totleSize += size;
}
//如果有第三方框架
//SDWebImage框架自身計(jì)算緩存的實(shí)現(xiàn)
// totleSize+=[[SDImageCache sharedImageCache] getSize]/1024.0/1024.0;
totleSize += [[SDImageCache sharedImageCache] getSize];
//8. 將文件夾大小轉(zhuǎn)換為 M/KB/B
NSString *totleStr = nil;
if (totleSize > 1000 * 1000){
totleStr = [NSString stringWithFormat:@"%.2fM",totleSize / 1000.00f /1000.00f];
}else if (totleSize > 1000){
totleStr = [NSString stringWithFormat:@"%.2fKB",totleSize / 1000.00f ];
}else{
totleStr = [NSString stringWithFormat:@"%.2fB",totleSize / 1.00f];
}
return totleStr;
}
#pragma mark - 清除path文件夾下緩存大小
+ (BOOL)clearCacheWithFilePath:(NSString *)path{
//拿到path路徑的下一級(jí)目錄的子文件夾
NSArray *subPathArr = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:nil];
NSString *filePath = nil;
NSError *error = nil;
for (NSString *subPath in subPathArr)
{
filePath = [path stringByAppendingPathComponent:subPath];
//刪除子文件夾
[[NSFileManager defaultManager] removeItemAtPath:filePath error:&error];
if (error) {
return NO;
}
}
//第三方框架
[[SDImageCache sharedImageCache] clearDiskOnCompletion:nil];
[[SDImageCache sharedImageCache] clearMemory];
//網(wǎng)易七魚的緩存清理
[[QYSDK sharedSDK] cleanResourceCacheWithBlock:^(NSError *error) {
}];
return YES;
}
使用
用起來(lái)還是很簡(jiǎn)單的,導(dǎo)入工具類被廓,寫好要清理的路徑做參數(shù)傳過(guò)去就好了坏晦。
//獲取路徑
//Document
NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
//library
NSString *libraryPath = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES)[0];
//library-caches
NSString *cachesPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
[ClearCacheTool getCacheSizeWithFilePath:path]