SDWebImageCache获取缓存大小并清除

本文介绍了如何使用SDWebImage库进行缓存管理,包括获取缓存大小、清除硬盘和内存缓存的方法。通过示例代码展示了不同单位的缓存大小转换,并提供了清除缓存的具体实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

##获取缓存大小

/// 获取缓存大小
 NSString * currentCacheSize = [NSString stringWithFormat:@"%@",[self fileSizeWithInteger:[[SDImageCache sharedImageCache] getSize]]];

// 根据数据计算出大小
- (NSString *) fileSizeWithInteger:(NSInteger)size{
    // 1K = 1024dB, 1M = 1024K,1G = 1024M
    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];
    }
}

清除SDWebImage缓存

/// 清除硬盘缓存-当前使用的也会清除
[[SDImageCache sharedImageCache] clearDisk];

/// 清除过期缓存,缓存时间为7天 计算当前缓存的大小和设置的最大缓存数比较,如果超出,就继续清除(___按照文件创建的先后顺序__)
[[SDImageCache sharedImageCache] cleanDisk];

///  清除内存缓存
[[[SDWebImageManager sharedManager] imageCache] clearMemory];  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值