#define CacheDirectory [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES) objectAtIndex:0]//程序缓存目录
#pragma mark - 清理缓存
- (void)clearCache{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSArray * files=[[NSFileManager defaultManager] subpathsAtPath:CacheDirectory];
for (NSString * p in files) {
NSError * error;
NSString * path=[CacheDirectory stringByAppendingPathComponent:p];
if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
[[NSFileManager defaultManager] removeItemAtPath:path error:&error];
}
}
[self performSelectorOnMainThread:@selector(clearCacheSuccess) withObject:nil waitUntilDone:YES];
});
}
float m=[self folderSizeAtPath:CacheDirectory];
cell.textLabel.text=[NSString stringWithFormat:@" %@ %.2fM",[moreArray objectAtIndex:indexPath.row],m];
#pragma mark - 遍历文件夹获得文件夹大小 返回多少M
- (float)folderSizeAtPath:(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];
}
return folderSize/(1024.0 * 1024.0);
}