清除缓存,,计算文件夹大小
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//
// NSLog(@"缓存文件大小为%@",[NSString stringWithFormat:@"%0.2fM",[self folderSizeAtPath:[NSString stringWithFormat:@"%@/Library/Caches/ImageCache",NSHomeDirectory()]]]);
#pragma mark 沙盒:Library(系统文件) Temp(临时文件) Documents(用于App缓存)
NSLog(@"===%@===",[NSString stringWithFormat:@"%0.2f",[self folderSizeAtPath:@"/Users/liushuo/Desktop/1567Pro第15天"]]);
//清除缓存
[self clearCacheFromPath:@"/Users/liushuo/Desktop/上线相关文档"];
//这里填写的是对应SDWebImage所缓存图片的目录
//可根据相应需求改成相应的文件目录
// Do any additional setup after loading the view, typically from a nib.
}
//单个文件的大小
- (long long) fileSizeAtPath:(NSString*) filePath{
NSFileManager* manager = [NSFileManager defaultManager];
if ([manager fileExistsAtPath:filePath]){
return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];
}
return 0;
}
//遍历文件夹获得文件夹大小,返回多少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);//转换为多少M进行返回
}
#pragma mark 清除缓存大小 打印NSHomeDritiony前往Documents进行查看路径
- (void)clearCacheFromPath:(NSString*)path{
//建立文件管理器
NSFileManager * manager = [NSFileManager defaultManager];
if ([manager fileExistsAtPath:path]) {
//如果文件路径存在 获取其中所有文件
NSArray * fileArr = [manager subpathsAtPath:path];//找到所有子文件的路径,存到数组中。
//首先需要转化为完整路径
//直接删除所有子文件
for (int i = 0; i < fileArr.count; i++) {
NSString * fileName = fileArr[i];
//完整路径
NSString * filePath = [path stringByAppendingPathComponent:fileName];
NSLog(@"%@",filePath);
[manager removeItemAtPath:filePath error:nil];
}
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
////////////////////////////////////////////////////////////
//融云的方法
//清理缓存
- (void)clearCache {
dispatch_async(
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//这里清除 Library/Caches 里的所有文件,融云的缓存文件及图片存放在 Library/Caches/RongCloud 下
NSString *cachPath = [NSSearchPathForDirectoriesInDomains(
NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSArray *files =
[[NSFileManager defaultManager] subpathsAtPath:cachPath];
for (NSString *p in files) {
NSError *error;
NSString *path = [cachPath stringByAppendingPathComponent:p];
if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
[[NSFileManager defaultManager] removeItemAtPath:path error:&error];
}
}
[self performSelectorOnMainThread:@selector(clearCacheSuccess)
withObject:nil
waitUntilDone:YES];
});
}
- (void)clearCacheSuccess {
UIAlertView *alertView =
[[UIAlertView alloc] initWithTitle:nil
message:@"缓存清理成功!"
delegate:nil
cancelButtonTitle:@"确定"
otherButtonTitles:nil, nil];
[alertView show];
}