1:方法一:缓存
第一:须要导入async
#import <SDImageCache.h>
#import <SDWebImageManager.h>ide
第二:执行下面的方法spa
#pragma mark - 清除缓存线程
- (void)clearCache {
float tmpSize = [[SDImageCache sharedImageCache] getSize];
NSString *clearMessage = tmpSize >= 1024 * 1024 ? [NSString stringWithFormat:@"清理缓存(%.2fM)" , tmpSize / 1024 / 1024] : [NSString stringWithFormat:@"清理缓存(%.2fK)", tmpSize / 1024];
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:clearMessage preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
}];
UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"肯定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[[SDImageCache sharedImageCache]clearDisk];
//清除内存缓存
[[[SDWebImageManager sharedManager] imageCache] clearMemory];
//清除系统缓存
[[NSURLCache sharedURLCache] removeAllCachedResponses];
}];
[alert addAction:action2];
[alert addAction:action1];
[self presentViewController:alert animated:YES completion:nil];
}code
方法2:推荐使用orm
在view上执行全部操做:图片
- (void)setIndexMain:(NSInteger)indexMain {
switch (indexMain) {
case 0:
self.leftTitleLabel.text = @"开启消息推送";
self.switchRight.hidden = NO;
self.rightTitleLabel.hidden = YES;
self.loadingView.hidden = YES;
break;
case 1:
self.leftTitleLabel.text = @"WIFI环境下显示高清图片";
self.switchRight.hidden = NO;
self.rightTitleLabel.hidden = YES;
self.loadingView.hidden = YES;
break;
case 2:
self.leftTitleLabel.text = @"清除缓存(正在计算缓存中...)";
self.switchRight.hidden = YES;
self.rightTitleLabel.hidden = YES;
self.loadingView.hidden = NO;
[self calculateCacheSize];
break;
default:
break;
}
}内存
/**
* 计算缓存大小
*/
- (void)calculateCacheSize {
dispatch_async(dispatch_get_global_queue(0, 0), ^{
// 获取缓存文件夹路径
unsigned long long size = YLCustomCacheFile.fileSize;
size += [SDImageCache sharedImageCache].getSize;
NSString *sizeText = nil;
if (size>=pow(10, 9)) { // size >= 1GB
sizeText = [NSString stringWithFormat:@"%.2fGB",size / pow(10, 9)];
}else if (size>=pow(10, 6)) { // 1GB > size >= 1MB
sizeText = [NSString stringWithFormat:@"%.2fMB",size / pow(10, 6)];
}else if (size>=pow(10, 3)) { // 1MB > size >= 1KB
sizeText = [NSString stringWithFormat:@"%.2fKB",size / pow(10, 3)];
}else { // 1KB > size
sizeText = [NSString stringWithFormat:@"%zdB",size];
}
// 生成文字
NSString *text = [NSString stringWithFormat:@"%@",sizeText];
// 回到主线程
dispatch_async(dispatch_get_main_queue(), ^{
self.rightTitleLabel.text = text;
self.rightTitleLabel.hidden = NO;
self.loadingView.hidden = YES;
self.leftTitleLabel.text = @"清除缓存";
});
});
}rem
- (void)setSelectedIndex:(NSInteger)selectedIndex {
_selectedIndex = selectedIndex;
if (selectedIndex == 2) {
[self clearCache];
}
}
/**
* 清除缓存大小
*/
- (void)clearCache {
if (![self.rightTitleLabel.text isEqualToString:@"0B"]) {
[self.viewController hudShowLoading:@"正在清除缓存..." toView:self.window];
}
// 清除SDWebImage
[[SDImageCache sharedImageCache] clearDiskOnCompletion:^{
dispatch_async(dispatch_get_global_queue(0, 0), ^{
// 清除自定义文件夹缓存
NSFileManager *mgr = [NSFileManager defaultManager];
[mgr removeItemAtPath:YLCustomCacheFile error:nil];
[mgr createDirectoryAtPath:YLCustomCacheFile withIntermediateDirectories:YES attributes:nil error:nil];
if (![self.rightTitleLabel.text isEqualToString:@"0B"]) {
[NSThread sleepForTimeInterval:1.0];
}
// 全部缓存清除完毕
dispatch_async(dispatch_get_main_queue(), ^{
// 隐藏指示器
[self.viewController hudHideToView:self.window];
// 设置文字
self.rightTitleLabel.text = @"0B";
self.rightTitleLabel.hidden = NO;
[self.viewController hudCustom:@"没有缓存了" withIcon:nil];
});
});
}];
}
友情提示:计算缓存大小:判断是否位文件仍是文件夹的两种方式
#pragma mark - 计算文件大小--方式1判断是文件夹仍是文件 - (unsigned long long)fileSize { // 总大小 unsigned long long size = 0; // 文件管理者 NSFileManager *mgr = [NSFileManager defaultManager]; // 文件属性 NSDictionary *attrs = [mgr attributesOfItemAtPath:self error:nil]; if ([attrs.fileType isEqualToString:NSFileTypeDirectory]) { //文件夹 // 得到文件夹大小,就是得到文件夹中全部文件的总大小 NSDirectoryEnumerator *enumerator = [mgr enumeratorAtPath:self]; for (NSString *subpath in enumerator) { // 全路径 NSString *fullSubpath = [self stringByAppendingPathComponent:subpath]; // 累加文件大小 size += [mgr attributesOfItemAtPath:fullSubpath error:nil].fileSize; } }else { // 文件 size = attrs.fileSize; } return size; }
#pragma mark - 计算文件大小--方式2判断是文件夹仍是文件 - (unsigned long long)fileSize { // 总大小 unsigned long long size = 0; // 文件管理者 NSFileManager *mgr = [NSFileManager defaultManager]; BOOL isDirectory = NO; BOOL exists = [mgr fileExistsAtPath:self isDirectory:&isDirectory]; if (!exists) return 0; if (isDirectory) { //文件夹 // 得到文件夹大小,就是得到文件夹中全部文件的总大小 NSDirectoryEnumerator *enumerator = [mgr enumeratorAtPath:self]; for (NSString *subpath in enumerator) { // 全路径 NSString *fullSubpath = [self stringByAppendingPathComponent:subpath]; // 累加文件大小 size += [mgr attributesOfItemAtPath:fullSubpath error:nil].fileSize; } }else { // 文件 size = [mgr attributesOfItemAtPath:self error:nil].fileSize; } return size; }