//PINCache
[[PINCache sharedCache]removeAllObjects];
[[PINMemoryCache sharedCache] removeAllObjects];
//[JJCacheManager clearPINRemoteImageManagerCache];
//YYCache
[[JJYYCacheManager defaultManager] removAllCache];
//SDWCashe
//[[JJSDWManager defaultManager] clearCash];
[[[SDWebImageManager sharedManager] imageCache] clearDisk];
最新版本clearDisk棄用,改為:[[[SDWebImageManager sharedManager] imageCache] clearDiskOnCompletion:nil];
[[[SDWebImageManager sharedManager] imageCache] clearMemory];
//webkitCache
[JJCacheManager clearWKWebKitCache];
#pragma mark - 獲取path路徑下資料夾大小
+ (NSString *)getCacheSizeWithFilePath:(NSString *)path;
#pragma mark - 清除path資料夾下快取大小 ps:webkit快取裡面有些內容沒有許可權刪除,需要用clearWKWebKitCache方法刪除
+ (BOOL)clearCacheWithFilePath:(NSString *)path;
#pragma mark - 刪除webkit快取
+ (BOOL)clearWKWebKitCache;
#pragma mark - pincahce中的快取需要手動刪除
+ (BOOL)clearPINRemoteImageManagerCache;
+ (BOOL)clearPINRemoteImageManagerCache {
NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
NSString *path = [cachesPath stringByAppendingPathComponent:@"/com.pinterest.PINDiskCache.PINRemoteImageManagerCache"];
//拿到path路徑的下一級目錄的子資料夾
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) {
JJLog(@"clearPINRemoteImageManagerCache : %@",error);
return NO;
}
}
return YES;
}
+ (BOOL)clearWKWebKitCache {
NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
NSString *path = [cachesPath stringByAppendingPathComponent:@"/WebKit"];
//拿到path路徑的下一級目錄的子資料夾
NSArray *subPathArr = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:nil];
NSString *filePath = nil;
NSError *error = nil;
for (NSString *subPath in subPathArr) {
filePath = [path stringByAppendingPathComponent:subPath];
// 其實主要就是這個沒有清除許可權
if (![filePath containsString:@"/Caches/Snapshots"]) {
//刪除子資料夾
[[NSFileManager defaultManager] removeItemAtPath:filePath error:&error];
}
if (error) {
JJLog(@"clearWKWebKitCache : %@",error);
return NO;
}
}
return YES;
}
#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. 拼接每一個檔案的全路徑
filePath =[path stringByAppendingPathComponent:subPath];
// 2. 是否是資料夾,預設不是
BOOL isDirectory = NO;
// 3. 判斷檔案是否存在
BOOL isExist = [[NSFileManager defaultManager] fileExistsAtPath:filePath isDirectory:&isDirectory];
// 4. 以上判斷目的是忽略不需要計算的檔案
if (!isExist || isDirectory || [filePath containsString:@".DS"]){
// 過濾: 1. 資料夾不存在 2. 過濾資料夾 3. 隱藏檔案
continue;
}
// 5. 指定路徑,獲取這個路徑的屬性
NSDictionary *dict = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil];
/**
attributesOfItemAtPath: 資料夾路徑
該方法只能獲取檔案的屬性, 無法獲取資料夾屬性, 所以也是需要遍歷資料夾的每一個檔案的原因
*/
// 6. 獲取每一個檔案的大小
NSInteger size = [dict[@"NSFileSize"] integerValue];
// 7. 計算總大小
totleSize += size;
}
//8. 將資料夾大小轉換為 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路徑的下一級目錄的子資料夾
NSArray *subPathArr = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:nil];
NSString *message = nil;
NSString *filePath = nil;
NSError *error = nil;
for (NSString *subPath in subPathArr)
{
filePath = [path stringByAppendingPathComponent:subPath];
// NSString *cacheSize = [JJCacheManager getCacheSizeWithFilePath:filePath];
// JJLog(@"%@\n\n%@\n",filePath,cacheSize);
//刪除子資料夾
[[NSFileManager defaultManager] removeItemAtPath:filePath error:&error];
if (error) {
return NO;
}
}
return YES;
}