作品鏈接:
http://www.reibang.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]) {
//設(shè)置cell右邊的指示器(用來說明正在處理一些事情)
UIActivityIndicatorView *loadingView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[loadingView startAnimating];
self.accessoryView = loadingView;
// 設(shè)置cell默認的文字(如果設(shè)置文字之前userInteractionEnabled=NO, 那么文字會自動變成淺灰色)
self.textLabel.text = @"清除緩存(正在計算緩存大小...)";
self.userInteractionEnabled = NO;
// 弱引用指向?qū)ο笏赖臅r候,它指向的對象就會生成空,就不會產(chǎn)生野指針瘪匿,不會產(chǎn)生壞內(nèi)存訪問。
// __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已經(jīng)銷毀了, 就直接返回
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(), ^{
// 設(shè)置文字
weakSelf.textLabel.text = test;
// 清空右邊的指示器
weakSelf.accessoryView = nil;
// 顯示右邊的箭頭
weakSelf.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
// 添加手勢監(jiān)聽器
[weakSelf addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clearCache)]];
// 恢復(fù)點擊事件
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];
// 設(shè)置文字
self.textLabel.text = @"清除緩存(0B)";
});
});
}];
}
/**
* 當cell重新顯示到屏幕上時, 也會調(diào)用一次layoutSubviews
*/
- (void)layoutSubviews
{
[super layoutSubviews];
// cell重新顯示的時候, 繼續(xù)轉(zhuǎn)圈圈
UIActivityIndicatorView *loadingView = (UIActivityIndicatorView *)self.accessoryView;
[loadingView startAnimating];
}