作品链接:
http://www.jianshu.com/users/1e0f5e6f73f6/top_articles
1.计算文件的大小
1.方法一
// 总大小
unsigned long long size = 0;
// 获得缓存文件夹路径
NSString *cachesPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject;
NSString *dirpath = [cachesPath stringByAppendingPathComponent:@"Asd"];
NSLog(@"%@",dirpath);
// 文件管理者
NSFileManager *mgr = [NSFileManager defaultManager];
// 获得文件夹的大小 == 获得文件夹中所有文件的总大小 contentsOfDirectoryAtPath: 只能获取一层
PHLog(@"contents - %@", [mgr contentsOfDirectoryAtPath:dirpath error:nil]);
// subpathsAtPath 能获取子层的
NSArray *subpaths = [mgr subpathsAtPath:dirpath];
for (NSString *subpath in subpaths) {
// 全路径
NSString *fullSubpath = [dirpath stringByAppendingPathComponent:subpath];
// 累加文件大小
size += [mgr attributesOfItemAtPath:fullSubpath error:nil].fileSize;
// NSDictionary *attrs = [mgr attributesOfItemAtPath:fullSubpath error:nil];
// size += [attrs[NSFileSize] unsignedIntegerValue];
}
NSLog(@"asd = %zd",size);
2.方法二
// 总大小
unsigned long long size = 0;
// 缓存路径文件夹
NSString *cachesPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject;
NSString *dirpath = [cachesPath stringByAppendingPathComponent:@"default"];
NSLog(@"%@",dirpath);
// 文件管理者
NSFileManager *manager = [NSFileManager defaultManager];
// 获得文件夹的大小 == 获得文件夹中所有文件的总大小
// Enumerator : 遍历器\迭代器
NSDirectoryEnumerator *enumerator = [manager enumeratorAtPath:dirpath];
for (NSString *subpath in enumerator ) {
// 全路径
NSString *fullSubpath = [dirpath stringByAppendingPathComponent:subpath];
// 累加文件大小
size += [manager attributesOfItemAtPath:fullSubpath error:nil].fileSize;
}
NSLog(@"%zd",size);
2.清除文件的缓存
- 宏定义
#define PHCustomCacheFile [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"Custom"]
1.计算大小
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
//设置cell右边的指示器(用来说明正在处理一些事情)
UIActivityIndicatorView *loadingView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[loadingView startAnimating];
self.accessoryView = loadingView;
// 设置cell默认的文字(如果设置文字之前userInteractionEnabled=NO, 那么文字会自动变成浅灰色)
self.textLabel.text = @"清除缓存(正在计算缓存大小...)";
self.userInteractionEnabled = NO;
// 弱引用指向对象死的时候,它指向的对象就会生成空,就不会产生野指针,不会产生坏内存访问。
// __weak PHClearCacheCell *weakSelf = self;
__weak typeof(self) weakSelf = self;
// 在子线程计算缓存大小
dispatch_async(dispatch_get_global_queue(0, 0), ^{
//#warning yanchi
// [NSThread sleepForTimeInterval:3.0];
// 获得缓存文件夹路径
unsigned long long size = PHCustomCacheFile.fileSize;
//unsigned long long size = @"/Users/apple/Desktop/Snip20160711_7.png".fileSize;
size += [SDImageCache sharedImageCache].getSize;
// 如果cell已经销毁了, 就直接返回
if (weakSelf == nil) return ;
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 *test = [NSString stringWithFormat:@"清除缓存(%@)",sizeText];
//回到主线程
dispatch_async(dispatch_get_main_queue(), ^{
// 设置文字
weakSelf.textLabel.text = test;
// 清空右边的指示器
weakSelf.accessoryView = nil;
// 显示右边的箭头
weakSelf.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
// 添加手势监听器
[weakSelf addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clearCache)]];
// 恢复点击事件
weakSelf.userInteractionEnabled = YES;
});
});
}
return self;
}
2.清除缓存
- (void)clearCache
{
// 弹出指示器
[SVProgressHUD showWithStatus:@"正在清除缓存..." maskType:SVProgressHUDMaskTypeBlack];
// 删除SDWebImage的缓存
[[SDImageCache sharedImageCache] clearDiskOnCompletion:^{
// 删除自定义的缓存
dispatch_async(dispatch_get_global_queue(0, 0), ^{
NSFileManager *manager = [NSFileManager defaultManager];
[manager removeItemAtPath:PHCustomCacheFile error:nil];
[manager createDirectoryAtPath:PHCustomCacheFile withIntermediateDirectories:YES attributes:nil error:nil];
//#warning 睡眠
// [NSThread sleepForTimeInterval:2.0];
// 所有的缓存都清除完毕
dispatch_async(dispatch_get_main_queue(), ^{
// 隐藏指示器
[SVProgressHUD dismiss];
// 设置文字
self.textLabel.text = @"清除缓存(0B)";
});
});
}];
}
/**
* 当cell重新显示到屏幕上时, 也会调用一次layoutSubviews
*/
- (void)layoutSubviews
{
[super layoutSubviews];
// cell重新显示的时候, 继续转圈圈
UIActivityIndicatorView *loadingView = (UIActivityIndicatorView *)self.accessoryView;
[loadingView startAnimating];
}