Sdwebimage, a third-party framework for image loading, is often used in development. App also has entry to clear the cache (only the image cache of SDWebImage is calculated here).
1. Import the header file first
#import <SDImageCache.h>
Copy the code
2. Method of use and calculation
Usage:
NSUInteger size = [[SDImageCache sharedImageCache] totalDiskSize]; NSString *cacheStr = [self fileSizeWithInterge:size]; self.cacheL.text = cacheStr;Copy the code
Calculation method:
- (NSString *)fileSizeWithInterge:(NSInteger)size {// 1k = 1024, If (size < 1024) {// less than 1k return [NSString stringWithFormat:@"%ldB",(long)size]; } else if (size < 1024 * 1024) {// less than 1M CGFloat cFloat = size / 1024; return [NSString stringWithFormat:@"%.1fK",cFloat]; } else if (size < 1024 * 1024 * 1024) {// < 1G CGFloat cFloat = size/(1024 * 1024); return [NSString stringWithFormat:@"%.1fM",cFloat]; } else {// greater than 1G CGFloat cFloat = size/(1024 * 1024 * 1024); return [NSString stringWithFormat:@"%.1fG",cFloat]; }}Copy the code
3. Clear the cache method, generally clear the cache can be directly zero, or directly in the query calculation
/ / clear the cache [MBProgressHUD showActivityMessageInWindow: @ "is clearing the cache"]. [[SDImageCache sharedImageCache] clearMemory]; [[SDImageCache sharedImageCache] clearDiskOnCompletion:^{ [MBProgressHUD hideHUD]; }];Copy the code
Note: If you enter the page, you will start to query the image cache. In the case of a large cache, the query may be slow, and the operation can be carried out in an asynchronous thread
WS(ws); dispatch_async(dispatch_get_main_queue(), ^{// Calculate SDWebImage cache size NSUInteger size = [[SDImageCache sharedImageCache] totalDiskSize]; NSString *cacheStr = [ws fileSizeWithInterge:size]; ws.cacheL.text = cacheStr; });Copy the code