获取缓存文件路径
(NSString *)getCachesPath{
// 获取Caches目录路径
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask,YES)
NSString *cachesDir = [paths objectAtIndex:0];
NSString *filePath = [cachesDir stringByAppendingPathComponent:@"myCache"]
return cachesDir
}
计算单个文件的大小
(long long)fileSizeAtPath:(NSString*)filePath{
NSFileManager* manager = [NSFileManager defaultManager];
if ([manager fileExistsAtPath:filePath]){
return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];
}
return 0;
}
遍历文件夹获得文件夹大小,返回多少M
(float)getCacheSizeAtPath:(NSString*)folderPath{
NSFileManager* manager = [NSFileManager defaultManager];
if (![manager fileExistsAtPath:folderPath]) return 0;
NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath:folderPath] objectEnumerator];
NSString* fileName;
long long folderSize = 0;
while ((fileName = [childFilesEnumerator nextObject]) != nil){
NSLog(@"fileName ==== %@",fileName);
NSString* fileAbsolutePath = [folderPath stringByAppendingPathComponent:fileName];
NSLog(@"fileAbsolutePath ==== %@",fileAbsolutePath);
folderSize += [self fileSizeAtPath:fileAbsolutePath];
}
NSLog(@"folderSize ==== %lld",folderSize);
return folderSize/(1024.0*1024.0);
}
清除缓存
+(void)clearCacheAtPath:(NSString *)path{
NSFileManager *fileManager=[NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:path]) {
NSArray *childerFiles=[fileManager subpathsAtPath:path];
for (NSString *fileName in childerFiles) {
NSString *absolutePath=[path stringByAppendingPathComponent:fileName];
[fileManager removeItemAtPath:absolutePath error:nil];
}
}
}