iOS 獲取快取大小與清除快取
阿新 • • 發佈:2019-01-26
首先要獲取cache資料夾路徑
#define cachePath [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject]
//
// LLFileTool.h
// BuDeJie
//
// Created by llkj on 2017/10/13.
// Copyright © 2017年 LayneCheung. All rights reserved.
// 處理檔案快取
#import <Foundation/Foundation.h>
@interface LLFileTool : NSObject
/**
刪除資料夾所有檔案
@param directoryPath 資料夾路徑
*/
+ (void)removeDirectoryPath:(NSString *)directoryPath;
/**
獲取資料夾尺寸
@param directoryPath 資料夾路徑
@param completion 資料夾尺寸
*/
+ (void)getFileSize:(NSString *)directoryPath completion:(void(^)(NSInteger totalSize))completion;
@end
//
// LLFileTool.m
// BuDeJie
//
// Created by llkj on 2017/10/13.
// Copyright © 2017年 LayneCheung. All rights reserved.
//
#import "LLFileTool.h"
@implementation LLFileTool
+ (void)removeDirectoryPath:(NSString *)directoryPath {
//獲取檔案管理者
NSFileManager *mgr = [NSFileManager defaultManager];
BOOL isDirectoey;
BOOL isExist = [mgr fileExistsAtPath:directoryPath isDirectory:&isDirectoey];
if (!isExist || !isDirectoey) {
NSException *exception = [NSException exceptionWithName:@"PathError" reason:@"需要傳入的是資料夾路徑,並且路徑要存在!" userInfo:nil];
[exception raise];
}
//獲取cache資料夾下所有檔案,不包括子路徑的子路徑
NSArray *subPaths = [mgr contentsOfDirectoryAtPath:directoryPath error:nil];
for (NSString *subPath in subPaths) {
//拼接完整路徑
NSString *filePath = [directoryPath stringByAppendingPathComponent:subPath];
//刪除路徑
[mgr removeItemAtPath:filePath error:nil];
}
}
+ (void)getFileSize:(NSString *)directoryPath completion:(void(^)(NSInteger totalSize))completion {
NSFileManager *mgr =[NSFileManager defaultManager];
BOOL isDirectory;
BOOL isExist = [mgr fileExistsAtPath:directoryPath isDirectory:&isDirectory];
if (!isDirectory || !isExist) {
NSException *exception = [NSException exceptionWithName:@"PathError" reason:@"需要傳入的是資料夾路徑,並且路徑要存在!" userInfo:nil];
[exception raise];
}
dispatch_async(dispatch_get_global_queue(0, 0), ^{
//獲取資料夾下所有檔案,包括子路徑的子路徑
NSArray *subPaths = [mgr subpathsAtPath:directoryPath];
NSInteger totalSize = 0;
for (NSString *subPath in subPaths) {
//獲取檔案全路徑
NSString *filePath = [directoryPath stringByAppendingPathComponent:subPath];
//判斷隱藏檔案
if ([filePath containsString:@".DS"]) continue;
//判斷是否資料夾
BOOL isDircetory;
//判斷檔案是否存在,並判斷是否是資料夾
BOOL isExist = [mgr fileExistsAtPath:filePath isDirectory:&isDircetory];
if (isDircetory || !isExist) continue;
//獲取檔案屬性
NSDictionary *attr = [mgr attributesOfItemAtPath:filePath error:nil];
NSInteger size = [attr fileSize];
totalSize += size;
}
//計算完成回撥
dispatch_sync(dispatch_get_main_queue(), ^{
if (completion) {
completion(totalSize);
}
});
});
}
@end
注意:返回的單位是(B),下面是單位換算
- (NSString *)cacheSizeStr {
NSInteger totalSize = _totalSize;
NSString *sizeStr = @"清除快取";
if (totalSize > 1000 * 1000) {
CGFloat sizeF = totalSize / 1000.0 / 1000.0;
sizeStr = [NSString stringWithFormat:@"%@(%.1fMB)", sizeStr, sizeF];
} else if (totalSize > 1000) {
CGFloat sizeF = totalSize / 1000.0;
sizeStr = [NSString stringWithFormat:@"%@(%.1fKB)", sizeStr, sizeF];
} else if (totalSize > 0) {
sizeStr = [NSString stringWithFormat:@"%@(%.ldB)", sizeStr, totalSize];
}
return sizeStr;
}