清除应用程序缓存

定义一个全局的float类型的cacheSize,用来记录缓存大小

- (void)_countCatcheSize

{

    //获取沙盒路径

    NSString *homePath = NSHomeDirectory();

    

    //拼接缓存路径

    NSString *imgPath = [homePath stringByAppendingPathComponent:@"Library/Caches/default/com.hackemist.SDWebImageCache.default"];

//    NSLog(@"imgPath is %@", imgPath);

    

    //文件管理

    NSFileManager *fileManager = [NSFileManager defaultManager];

    

    //取得一个文件夹下所有的文件路径

    NSError *error = nil;

    NSArray *subPath = [fileManager subpathsOfDirectoryAtPath:imgPath error:&error];

    

    //内存大小

    long long sum = 0;

    for (NSString *filePath in subPath) {

        //取得一个文件的路径

        NSString *path = [imgPath stringByAppendingPathComponent:filePath];

        

        //拿到文件的属性

        NSDictionary *attributes = [fileManager attributesOfItemAtPath:path error:&error];

        

        NSNumber *fileSize = attributes[NSFileSize];

        

        sum += [fileSize longLongValue];

    }

    

    //计算内存

    cacheSize = sum / (1024.0 * 1024);

    NSLog(@"------%.2f", cacheSize);

}


//单元格出现时记录缓存数据

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

{

    if (indexPath.row == 0) {

        [self _countCatcheSize];

        UILabel *label = (UILabel *)[cell viewWithTag:100];

        label.text = [NSString stringWithFormat:@"%.1fM", cacheSize];

    }

}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

    if (indexPath.row == 0) {

        //弹出清除对话框

        UIAlertController *alertCtrl = [UIAlertController alertControllerWithTitle:@"提示" message:@"确定清除缓存?" preferredStyle:UIAlertControllerStyleAlert];

        

        //取消按钮

        UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];

        

        //确定按钮

        UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

            //确定清空缓存

            [[SDImageCache sharedImageCache] clearDisk];

            

            //刷新tableView

            [self.tableView reloadData];

        }];

        

        [alertCtrl addAction:cancleAction];

        [alertCtrl addAction:sureAction];

        //弹出对话框

        [self presentViewController:alertCtrl animated:YES completion:nil];

    }

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值