/**
获取缓存路径
@return
*/
- (NSString *)getCachesPath{
// 获取Caches目录路径
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask,YES);
NSString *cachesDir = [paths objectAtIndex:0];
// NSString *filePath = [cachesDir stringByAppendingPathComponent:@"myCache"];
return cachesDir;
}
/**
计算单个文件的大小
@param filePath
@return
*/
- (long long)fileSizeAtPath:(NSString*)filePath{
NSFileManager* manager = [NSFileManager defaultManager];
if ([manager fileExistsAtPath:filePath]){
return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];
}
return 0;
}
/**
计算文件夹的大小
@param folderPath
@return
*/
- (NSString *)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){
NSString* fileAbsolutePath = [folderPath stringByAppendingPathComponent:fileName];
folderSize += [self fileSizeAtPath:fileAbsolutePath];
}
NSString *cacheStr = @"";
if (folderSize/(1024.0*1024.0)>=1) {
cacheStr = [NSString stringWithFormat:@"%0.2fMb",folderSize/(1024.0*1024.0)];
}else if (folderSize/(1024.0*1024.0)>0){
cacheStr = [NSString stringWithFormat:@"%lldkb",folderSize/1024];
}else {
cacheStr = @"0kb";
}
return cacheStr;
}
/**
清除缓存文件
@param path
*/
- (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];
}
}
}
// 清除缓存的转圈动画
UIActivityIndicatorView *loading = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
loading.frame = CGRectMake(dataLabel.frame.size.width-20, 5, 20, 20);
[dataLabel addSubview:loading];
[loading startAnimating];
// __block typeof(self) weakSelf;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[loading stopAnimating];
[_table reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationNone];
});