1.使用第三方框架sdWebImage 下載的圖片,要計(jì)算出他的大小,使用
#import <SDImageCache.h>
NSUInteger cacheSize = [[SDImageCache sharedImageCache]getSize];
獲取文件夾緩存原理:
1.獲取文件夾路徑
2.遍歷子文件,拼接全文件名
3.通過(guò)文件管理類獲取文件信息 dict.fileSize
-(unsigned long long)getFileSize:(NSString *)directoryPath
{
//1.獲取緩存文件夾
NSString * cachePath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
//2.拼接文件夾路徑
NSString * filePath = [cachePath stringByAppendingPathComponent:directoryPath];
//3.判斷是否文件夾 是 遍歷子文件夾,算出文件總大小; 否 算出當(dāng)前文件夾
NSFileManager * mgr = [NSFileManager defaultManager];
unsigned long long cacheSize = 0;
BOOL isDirectory = NO;
BOOL isExist= [mgr fileExistsAtPath:filePath isDirectory:&isDirectory];
if ( ! isExist)
{
NSException * exception = [NSException exceptionWithName:@"非法路徑" reason:@"路徑不存在" userInfo:nil];
[exception raise];
}
if (isDirectory) {
NSDirectoryEnumerator * enumerator = [mgr enumeratorAtPath:filePath];
for (NSString * subPath in enumerator) {
NSString * fullPath = [filePath stringByAppendingPathComponent:subPath];
NSDictionary * dict = [mgr attributesOfItemAtPath:fullPath error:nil];
cacheSize += dict.fileSize;
}
}
else {
cacheSize = [mgr attributesOfItemAtPath:filePath error:nil].fileSize;
}
return cacheSize;
}
處理不同cell共存,將清除緩存的cell抽成自定義cell
static NSString * const LMClearCellID = @"LMClearCellID";
@implementation LMSettingController
- (void)viewDidLoad {
[super viewDidLoad];
[self.tableView registerClass:[LMClearCell class]forCellReuseIdentifier:LMClearCellID];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
return [tableView dequeueReusableCellWithIdentifier:LMClearCellID];
}
換算單位
-(NSString *) cacheStr
{
if (_cacheStr == nil) {
if (_cacheSize > pow(10, 9)) {
_cacheStr = [NSString stringWithFormat:@"清除緩存(%.2fGB)",_cacheSize / pow(10, 9)];
}
else if (_cacheSize > pow(10, 6)){
_cacheStr = [NSString stringWithFormat:@"清除緩存(%.2fMB)",_cacheSize / pow(10, 6)];
}
else if (_cacheSize > pow(10, 3)){
_cacheStr = [NSString stringWithFormat:@"清除緩存(%.2fKB)",_cacheSize / pow(10, 3)];
}
else
{
_cacheStr = [NSString stringWithFormat:@"清除緩存(%zdB)",_cacheSize];
}
}
return _cacheStr;
}
開啟子線程計(jì)算大小
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
self.textLabel.text = @"正在計(jì)算大小...";
self.userInteractionEnabled = NO;
__weak typeof (self) weakSelf = self;
//開啟子線程計(jì)算大小
dispatch_async(dispatch_get_global_queue(0, 0), ^{
sleep(5);
NSString * cachePath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
NSString * filePath = [cachePath stringByAppendingPathComponent:@"default"];
weakSelf.cacheSize = [LMFileCacheManger getFileSize:filePath];
if (weakSelf == nil) return ;
dispatch_async(dispatch_get_main_queue(), ^{
weakSelf.textLabel.text = weakSelf.cacheStr;
weakSelf.userInteractionEnabled = YES;
});
});
}
return self;
}
點(diǎn)擊cell刪除緩存
1.先刪除SDWebImage的緩存,再刪除自己的緩存
2.根據(jù)需要重新創(chuàng)建自己的文件夾
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
if ([cell isKindOfClass:[LMClearCell class]]) {
LMClearCell * clearCell = (LMClearCell *)cell;
[clearCell clearCache];
}
}
---
-(void)clearCache
{
//先刪除SDWebImage的緩存
[[SDImageCache sharedImageCache] clearDiskOnCompletion:^{
NSString * path = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
NSLog(@"%@",path);
NSString* fullpath = [path stringByAppendingPathComponent:@"custom"];
[LMFileCacheManger clearFileCache:fullpath]; //刪除自己的文件夾
dispatch_async(dispatch_get_main_queue(),^{//回到主線程刷新UI
self.textLabel.text = @"清除緩存(0B)";
});
}];
}
---
+(void)clearFileCache:(NSString *)filePath
{
[SVProgressHUD showWithStatus:@"正在刪除"];
[SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
NSFileManager * mgr = [NSFileManager defaultManager];
dispatch_async(dispatch_get_global_queue(0, 0), ^{
[mgr removeItemAtPath:filePath error:nil];
dispatch_async(dispatch_get_main_queue(), ^{
[SVProgressHUD showSuccessWithStatus:@"刪除成功"];
});
});
}
用戶體驗(yàn)細(xì)節(jié)
1.如果緩存文件非常大,那么應(yīng)該開啟子線程下載
2.當(dāng)前正在計(jì)算過(guò)程中,cell不能被點(diǎn)擊
3.當(dāng)用戶在計(jì)算緩存過(guò)程中,點(diǎn)擊返回按鈕,退出當(dāng)前控制器,那么無(wú)需再回到主線程刷新UI
// 如果cell已經(jīng)銷毀了, 就直接返回
if (weakSelf == nil) return;
遇到的bug
1.設(shè)置cell默認(rèn)的文字(如果設(shè)置文字之前userInteractionEnabled=NO, 那么文字會(huì)自動(dòng)變成淺灰色)
self.textLabel.text = @"清除緩存(正在計(jì)算緩存大小...)";
self.userInteractionEnabled = NO;
2.block 強(qiáng)引用(block會(huì)對(duì)外部對(duì)象進(jìn)行強(qiáng)引用,直到block執(zhí)行完才會(huì)銷毀)
__weak typeof(self) weakSelf = self; //__weak修飾
相關(guān)博客:
http://www.reibang.com/p/76614766b2ea
http://www.cnblogs.com/pengyingh/articles/2350345.html