iOS 計(jì)算緩存與清楚緩存


#define CachePath [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject]

一般要清楚的就是上面的緩存,傳入上面路徑。
+ (void)getFileSize:(NSString *)directoryPath completion:(void(^)(NSInteger))completion
{
    
    // 獲取文件管理者
    NSFileManager *mgr = [NSFileManager defaultManager];
    BOOL isDirectory;
    BOOL isExist = [mgr fileExistsAtPath:directoryPath isDirectory:&isDirectory];
    
    if (!isExist || !isDirectory) {
        // 拋異常
        // name:異常名稱(chēng)
        // reason:報(bào)錯(cuò)原因
       NSException *excp = [NSException exceptionWithName:@"pathError" reason:@"需要傳入的是文件夾路徑,并且路徑要存在" userInfo:nil];
        [excp raise];
        
    }
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        
        // 獲取文件夾下所有的子路徑,包含子路徑的子路徑
        //查找給定路徑下的所有子路徑。深度查找,不限于當(dāng)前層,也會(huì)查找package的內(nèi)容
        NSArray *subPaths = [mgr subpathsAtPath:directoryPath];
        
        NSInteger totalSize = 0;
        
        for (NSString *subPath in subPaths) {
            // 獲取文件全路徑
            NSString *filePath = [directoryPath stringByAppendingPathComponent:subPath];
            
            // 判斷隱藏文件
            if ([filePath containsString:@".DS"]) continue;
            
            // 判斷是否文件夾
            BOOL isDirectory;
            // 判斷文件是否存在,并且判斷是否是文件夾
            BOOL isExist = [mgr fileExistsAtPath:filePath isDirectory:&isDirectory];
            if (!isExist || isDirectory) continue;
            
            // 獲取文件屬性
            // attributesOfItemAtPath:只能獲取文件尺寸,獲取文件夾不對(duì),
            NSDictionary *attr = [mgr attributesOfItemAtPath:filePath error:nil];
            
            // 獲取文件尺寸
            NSInteger fileSize = [attr fileSize];
            
            totalSize += fileSize;
        }
        
        // 計(jì)算完成回調(diào)
        dispatch_sync(dispatch_get_main_queue(), ^{
            if (completion) {
                completion(totalSize);
            }
        });
        
        

    });
    
    
}


+ (void)removeDirectoryPath:(NSString *)directoryPath
{
    // 獲取文件管理者
    NSFileManager *mgr = [NSFileManager defaultManager];
    
    BOOL isDirectory;
    BOOL isExist = [mgr fileExistsAtPath:directoryPath isDirectory:&isDirectory];
    
    if (!isExist || !isDirectory) {
        // 拋異常
        // name:異常名稱(chēng)
        // reason:報(bào)錯(cuò)原因
        NSException *excp = [NSException exceptionWithName:@"pathError" reason:@"需要傳入的是文件夾路徑,并且路徑要存在" userInfo:nil];
        [excp raise];
        
    }
    
    // 獲取cache文件夾下所有文件,不包括子路徑的子路徑
    NSArray *subPaths = [mgr contentsOfDirectoryAtPath:directoryPath error:nil];
    
    for (NSString *subPath in subPaths) {
        // 拼接完成全路徑
        NSString *filePath = [directoryPath stringByAppendingPathComponent:subPath];
        
        // 刪除路徑
        [mgr removeItemAtPath:filePath error:nil];
    }

}
最后編輯于
?著作權(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)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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