SDWebImage计算缓存值与清除缓存

显示缓存值:
UILabel *cacheLabel = [[UILabel alloc] initWithFrame:CGRectMake(JKScreenW - 140, 0, 100, 40)];
[cell addSubview:cacheLabel];
cacheLabel.font = [UIFont jk_systemFontOfPxSize:28];
cacheLabel.textColor = JKSameRGBColor(153);
cacheLabel.textAlignment = NSTextAlignmentRight;
//获取缓存大小 
NSUInteger intg = [[SDImageCache sharedImageCache] getSize];
//将缓存的大小转换为带单位的数值
NSString *currentVolum = [NSString stringWithFormat:@"%@",[self fileSizeStringWithInterge:intg]];
cacheLabel.text = currentVolum;
_cacheLabel = cacheLabel;
//换为带单位的数值
- (NSString *)fileSizeStringWithInterge:(NSInteger)size{
    // 1k = 1024, 1m = 1024k
    if (size < 1024) {// 小于1k
        return [NSString stringWithFormat:@"%ldB",(long)size];
    }else if (size < 1024 * 1024){// 小于1m
        CGFloat aFloat = size/1024;
        return [NSString stringWithFormat:@"%.0fK",aFloat];
    }else if (size < 1024 * 1024 * 1024){// 小于1G
        CGFloat aFloat = size/(1024 * 1024);
        return [NSString stringWithFormat:@"%.1fM",aFloat];
    }else{
        CGFloat aFloat = size/(1024*1024*1024);
        return [NSString stringWithFormat:@"%.1fG",aFloat];
    }
}
清除缓存:
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"是否清除缓存" message:nil preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

      //清除缓存
      [[SDImageCache sharedImageCache] clearDiskOnCompletion:nil];
      [[SDImageCache sharedImageCache] clearMemory];//可不写
      [JKProgressHUD showSuccessWithStatus:@"清除缓存成功!"];//JKProgressHUD继承自SVProgressHUD
      _cacheLabel.text = @"";

}];
[alertController addAction:cancelAction];
[alertController addAction:sureAction];
[self presentViewController:alertController animated:NO completion:nil];
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值