1. 计算图片缓存大小
- (void)imageCacheSize:(NSInteger) accuracy
{
dispatch_async(dispatch_get_global_queue(0, 0), ^{
CGFloat imageSize = ([[SDImageCache sharedImageCache] getSize] / 1024.0f) / 1024.0f;
NSString *imageSizeString = [NSString stringWithFormat:@"%f",imageSize];
NSRange dotRange = [imageSizeString rangeOfString:@"."];
NSInteger lastPosition = dotRange.location + accuracy;
imageSizeString = [imageSizeString substringToIndex:lastPosition];
dispatch_async(dispatch_get_main_queue(), ^{
self.cacheLabel.text = [NSString stringWithFormat:@"%@ M",imageSizeString];
});
});
}
2. 清除缓存
- (void)goToClearCache
{
[NSThread detachNewThreadSelector:@selector(clearCache) toTarget:self withObject:nil];
//loading
[self performSelector:@selector(clearCacheFinished) withObject:nil afterDelay:1];
}
- (void)clearCache
{
[[SDImageCache sharedImageCache] clearDisk];
[[SDImageCache sharedImageCache] clearMemory];
}
- (void)clearCacheFinished
{
//清除成功
//缓存大小清空
self.cacheLabel.text = @"0 M";
}