博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
IOS 获取缓存目录文件大小并清除
阅读量:6939 次
发布时间:2019-06-27

本文共 3993 字,大约阅读时间需要 13 分钟。

1.获取缓存目录

//获取缓存文件路径-(NSString *)getCachesPath{    // 获取Caches目录路径    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);    NSString *cachesDir = [paths objectAtIndex:0];        //指定文件名   // NSString *filePath = [cachesDir stringByAppendingPathComponent:@"com.nickcheng.NCMusicEngine"];        return cachesDir;}

 

2.计算缓存文件大小

//计算缓存文件的大小- (long long) fileSizeAtPath:(NSString*) filePath{    NSFileManager* manager = [NSFileManager defaultManager];    if ([manager fileExistsAtPath:filePath]){        //        //取得一个目录下得所有文件名//        NSArray *files = [manager subpathsAtPath:filePath];//        NSLog(@"files1111111%@ == %ld",files,files.count);//        //        // 从路径中获得完整的文件名(带后缀)//        NSString *exe = [filePath lastPathComponent];//        NSLog(@"exeexe ====%@",exe);//        //        // 获得文件名(不带后缀)//        exe = [exe stringByDeletingPathExtension];//        //        // 获得文件名(不带后缀)//        NSString *exestr = [[files objectAtIndex:1] stringByDeletingPathExtension];//        NSLog(@"files2222222%@  ==== %@",[files objectAtIndex:1],exestr);                        return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];    }        return 0;}

清除缓存文件

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(@"清理成功");}

----------------------------------------------------------------------------------------------

dispatch_async(                       dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)                       , ^{                           //获取缓存目录                           NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES) objectAtIndex:0];                                                      NSError *error;                           if ([[NSFileManager defaultManager] fileExistsAtPath:cachPath]) {                               //如果存在就删除                               [[NSFileManager defaultManager] removeItemAtPath:cachPath error:&error];                           }                                                      [self performSelectorOnMainThread:@selector(clearCacheSuccess) withObject:nil waitUntilDone:YES];                       });//计算缓存文件的大小,多少M- (NSString *) fileSizeAtPath:(NSString*)cachPath{    NSFileManager* manager = [NSFileManager defaultManager];    if ([manager fileExistsAtPath:cachPath]){        unsigned long long length  = [[manager attributesOfItemAtPath:cachPath error:nil] fileSize];        float ff = length/1024.0/1024.0; //换算成多少M        NSString *size = [NSString stringWithFormat:@"%0.2fM",ff];        return size;    }    return 0;}//清理缓存后刷新-(void)clearCacheSuccess{    NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES) objectAtIndex:0];    NSString *size = [self fileSizeAtPath:cachPath];    _setting.cache = size;    [self.tableView reloadData];        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"清理成功" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];    [alertView show];}

 

转载于:https://www.cnblogs.com/ZhangYuGe/p/4259765.html

你可能感兴趣的文章
Spring Shiro
查看>>
递归求组合数
查看>>
小蚂蚁学习数据结构(10)——树的基本介绍
查看>>
域环境迁移
查看>>
FastDFS安装使用实战一(安装篇)
查看>>
我的友情链接
查看>>
更改DEB包内容
查看>>
我的友情链接
查看>>
ibatis动态语句中的prepend
查看>>
keepalived实现LVS的高可用以及实现web服务的高可用(主从模型、双主模型)
查看>>
linux apache
查看>>
Mysql DBA 高级运维学习之路-删除表中数据
查看>>
4.26日第14次作业,23章项目整体绩效评估,24-32章信息安全相关知识
查看>>
新一代java模板引擎典范 Beetl
查看>>
centos6.8+nginx搭建简单的https服务器
查看>>
cut,sort,wc,uniq,tee,tr,split,并且,和,或者
查看>>
LVS负载均衡之三:LVS-DR搭建web群集、LVS结合Keepalived搭建高可用web群集
查看>>
JavaScript 堆内存分析新工具 OneHeap
查看>>
浅谈java异常机制
查看>>
Docker 监控之 SaaS 解决方案
查看>>