清理沙盒缓存&获取缓存size
2015-09-16 18:06
288 查看
清理沙盒缓存
1、清理当前沙盒中的缓存信息
dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) , ^{ NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES) objectAtIndex:0]; NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:cachPath]; NSLog(@"files :%d",[files count]); 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:nilwaitUntilDone:YES];}); -(void)clearCacheSuccess { NSLog(@"清理成功"); }
2、SDImage第三方清除缓存的方法: (清除的一般是图片的缓存)
[[SDImageCache sharedImageCache] clearDisk]; //清楚磁盘缓存 [[SDImageCache sharedImageCache] clearMemory]; //清楚内存缓存
获取缓存Size
1、获取cache中当前缓存数据的大小
NSString *filenameNavigationBar=[CacheDownMessage getTargetPathWithBasepath:@"listData.plist"];//获取路径 NSDictionary * dictNavigationBar = [[NSFileManager defaultManager] attributesOfItemAtPath:filenameNavigationBar error:nil]; NSString *stringNavigationBarLength = [dictNavigationBar objectForKey:NSFileSize];
2.1、计算文件大小
- (long long) fileSizeAtPath:(NSString*) filePath{ NSFileManager* manager = [NSFileManager defaultManager]; if ([manager fileExistsAtPath:filePath]){ return [[manager attributesOfItemAtPath:filePath error:nil] fileSize]; } return 0; }
2.2、计算文件大小
//遍历文件夹获得文件夹大小,返回多少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); }
获取cache文件夹中文件个数
NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:cachPath]; NSLog(@"files :%d",[files count]);
/article/2153109.html
相关文章推荐
- 中级篇——Dijkstra算法求最短路径
- 尴尬的年龄,尴尬的位置。
- python读取excel的内容
- JVectorMap地图插件.Net版开源
- C#调用耗时函数时显示进度条方式
- android事件分发机制详解
- 在Linux执行命令报错”Arg list too long”的原因分析
- yii2.0给表单添加验证码的实现方法
- MySQL 使用存储过程实现留存率
- logstash 与 inode 以及 python 文件操作
- 查处每门功课前两名学生的名字,课程名,成绩
- RabbitMQ > 核心概念
- html5 canvas画椭圆形
- Scanner的使用
- Ubuntu14.04+KVM配置虚拟机桥接(bridge)
- 答同学提问
- 在Mac上安装Sublime Text3的插件
- JavaScript(内置对象)
- session的生命周期
- 用三次贝塞尔曲线拟合圆弧