您的位置:首页 > 移动开发 > IOS开发

[一句秒懂]ios 清除缓存

2016-05-27 00:00 441 查看
摘要: 清除缓存、ios

1:方法一:

第一:需要导入

#import <SDImageCache.h>
#import <SDWebImageManager.h>

第二:执行下面的方法

#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];
}

方法2:推荐使用

在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 = @"清除缓存";
});
});
}

- (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];
});
});

}];
}

友情提示:计算缓存大小:判断是否位文件还是文件夹的两种方式

[code=language-objectivec]#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;
}


[code=language-objectivec]#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息