zoukankan      html  css  js  c++  java
  • iOS-缓存大小显示功能和一键清理功能

      iAronTalk Blog opens.

      If you judge people, you have no time to love them.

    -=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

      缓存占用了系统的大量空间,如何实时动态的显示缓存的大小,使用户清晰的了解缓存的积累情况,有效的进行一键清理呢?

      为方便读者和未来自己更好理解,我们创建这样场景。(在表视图的清除缓存一单元格内创建一个UILabel *cacheLabel用于显示当前缓存,当点击单元格弹出提示框,点击确定,清除缓存)。

      下面是实现代码:

     1 #pragma mark - 计算缓存大小
     2 - (NSString *)getCacheSize
     3 {
     4     //定义变量存储总的缓存大小
     5     long long sumSize = 0;
     6     
     7     //01.获取当前图片缓存路径
     8     NSString *cacheFilePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches"];
     9     
    10     //02.创建文件管理对象
    11     NSFileManager *filemanager = [NSFileManager defaultManager];
    12     
    13         //获取当前缓存路径下的所有子路径
    14     NSArray *subPaths = [filemanager subpathsOfDirectoryAtPath:cacheFilePath error:nil];
    15 //遍历所有子文件 16 for (NSString *subPath in subPaths) { 17 //1).拼接完整路径 18 NSString *filePath = [cacheFilePath stringByAppendingFormat:@"/%@",subPath]; 19 //2).计算文件的大小 20 long long fileSize = [[filemanager attributesOfItemAtPath:filePath error:nil]fileSize]; 21 //3).加载到文件的大小 22 sumSize += fileSize; 23 } 24 float size_m = sumSize/(1000*1000); 25 return [NSString stringWithFormat:@"%.2fM",size_m]; 26 27 } 28 #pragma mark - 清除缓存提示(UITableViewDataSourceDelegate) 29 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 30 { 31 if (indexPath.row == 0) { 32 UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"缓存清除" message:@"确定清除缓存?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定",nil]; 33 [alertView show]; 34 } 35 } 36 #pragma mark - UIAlertViewDelegate方法实现 37 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 38 { 39 NSLog(@"代码执行到此"); 40 //判断点击的是确认键 41 if (buttonIndex == 1) { 42 //01...... 43 NSFileManager *fileManager = [NSFileManager defaultManager]; 44 //02..... 45 NSString *cacheFilePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches"]; 46 //03...... 47 [fileManager removeItemAtPath:cacheFilePath error:nil]; 48 49 //04刷新第一行单元格 50 NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:0]; 51 [_tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; 52 53 //05 :04和05使用其一即可 54 [_tableView reloadData];//刷新表视图 55 } 56 @pragma -mark -放置于.m文件首段较为合适,本DEMO仅做功能性展示,实时监测缓存大小,从其他界面跳转到本页面,也需要刷新下表视图 57 - (void)viewWillAppear:(BOOL)animated 58 { 59 [super viewWillAppear:YES]; 60 [_tableView reloadData]; 61 }

       由于编者水平有限,不妥之处在所难免,恳请各个大牛批评指正,提出宝贵建议。

    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    【题解】洛谷P4391 [BOI2009] Radio Transmission(KMP)
    【题解】UVA10298 Power String(KMP)
    【题解】洛谷P4281 [AHOI2008] 紧急集合(求三个点LCA)
    [BZOJ4712]洪水-[树链剖分+线段树]
    [BZOJ2961]共点圆-[凸包+cdq分治]
    [BZOJ1185][HNOI2007]最小矩形覆盖-[凸包+旋转卡壳]
    [BZOJ2738]矩阵乘法-[整体二分+树状数组]
    [POJ2104]Kth Number-[整体二分]
    [arc082F]Sandglass
    [arc076F]Exhausted?
  • 原文地址:https://www.cnblogs.com/iAronTalk/p/4770819.html
Copyright © 2011-2022 走看看