這篇文章主要介紹<ios清理緩存>方面的知識.主要為剛接觸ios的小白提供思路,有誤之處請大家多多指教--->>>新鮮出爐的.
1,清理緩存?zhèn)€人理解
有個很牛逼的加載圖片的框架SDWebImage,大家應(yīng)該都知道的.我也看過很多人寫的清理緩存就是清理SDWebImage里面的緩存,那么如果我們系統(tǒng)里面有除了圖片之外的加載了???SDWebImage就不可能幫助你完成你需要的計算了,因為框架內(nèi)部"com.hackemist.SDWebImageCache"這個地址進(jìn)行清理緩存.那么我們自己的網(wǎng)頁緩存和視頻緩存還是清理不了,所以清理緩存如果是用了SDWebImage框架必須清理兩部分(自己的緩存和SDWebImage的緩存),若果不用框架就直接清理自己的緩存就行了.
2,先看看效果,我主要用的是系統(tǒng)的tableViewCell控件做的,大家可以根據(jù)自己的喜好加以修改:
計算清理緩存.png
緩存計算完成.png
緩存刪除完成.png
3,封裝獲取緩存的文件的大小
//總大小
unsigned long long size = 0;
//獲取管理文件的對象
NSFileManager *manager = [NSFileManager defaultManager];
//判斷是否是文件還是文件夾
BOOL isDirectory = NO;
//獲取文件夾是否存在
BOOL exists = [manager fileExistsAtPath:self isDirectory:&isDirectory];
if (!exists) return size;
if (isDirectory) {
//獲得文件的大下
NSDirectoryEnumerator *enumerator = [manager enumeratorAtPath:self];
//遍歷文件遍歷器
for (NSString *subpath in enumerator) {
//全路徑
NSString *fullPath = [self stringByAppendingPathComponent:subpath];
//累加文件夾中所有文件的大小
size += [manager attributesOfItemAtPath:fullPath error:nil].fileSize;
}
}else{
//獲取文件的大小
size = [manager attributesOfItemAtPath:self error:nil].fileSize;
}
4,封裝清理緩存的cell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
//設(shè)置cell右邊的指示器(用來說明正在處理事情)
UIActivityIndicatorView *indicatorView = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
//開啟菊花
[indicatorView startAnimating];
//設(shè)置cell的accessoryView
self.accessoryView = indicatorView;
//設(shè)置cell的默認(rèn)顯示文字
self.textLabel.text = @"清除緩存(正在計算緩存大小...)";
//取消用戶點擊監(jiān)聽
self.userInteractionEnabled = NO;
__weak typeof(self) weakself = self;
//計算文件的打下
dispatch_async(dispatch_get_global_queue(0, 0), ^{
//計算緩存文件的大小是耗時操作,最好放在子線程中執(zhí)行
unsigned long long size = @"/Users/bobo/Desktop/text".cacheFileSize;
//SDImageCache的緩存大小
//[SDImageCache sharedImageCache].getSize是SDImageImage框架獲取它自己加載的緩存大小,有興趣的同學(xué)可以進(jìn)文件看下.
size += [SDImageCache sharedImageCache].getSize;
//如果cell已經(jīng)銷毀就直接返回
if (weakself == nil) return ;
//獲取轉(zhuǎn)換后的size大小
//cacheTextForSize這個轉(zhuǎn)換大家就去自己動手寫寫吧,用pow寫很簡單,不做詳細(xì)介紹了
NSString *cellText = [weakself cacheTextForSize:size];
/**
之所以用異步線程,因為計算文件大小是耗時操作文門應(yīng)該放到子線程中,不能影響UI的顯示.
*/
//主線程做UI工作
dispatch_async(dispatch_get_main_queue(), ^{
//設(shè)置文字
weakself.textLabel.text = cellText;
//size計算完成后,設(shè)置accessoryView為空
weakself.accessoryView = nil;
//重新設(shè)置cell的右邊為箭頭狀態(tài)
weakself.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clearCache)];;
[ weakself addGestureRecognizer:tap];
//狀態(tài)完成后,回復(fù)點擊效果
self.userInteractionEnabled = YES;
});
});
}
return self;
}
5,清理緩存
//清理緩存
- (void)clearCache{
__weak typeof(self)weakself = self;
//顯示提醒
[SVProgressHUD showWithStatus:@"正在清理緩存..."];
[SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
//刪除SDImage的緩存
//clearDiskOnCompletion這里用這個完成Block主要是因為SDImageCache的clearDisk方法是異步子線程,我們的dispatch_get_global_queue也是異步子線程,這也可能造成主線程卡死.所以選在先讓框架的刪除,在做自己的事情.
[[SDImageCache sharedImageCache] clearDiskOnCompletion:^{
//清除自定義路徑下的文件內(nèi)容
dispatch_async(dispatch_get_global_queue(0, 0), ^{
NSFileManager *manager = [NSFileManager defaultManager];
//移除緩存文件
[manager removeItemAtPath:cacheFilePath(customFilePath) error:nil];
//創(chuàng)建緩存文件
[manager createDirectoryAtPath:cacheFilePath(customFilePath) withIntermediateDirectories:YES attributes:nil error:nil];
dispatch_async(dispatch_get_main_queue(), ^{
//移除提示
[SVProgressHUD dismiss];
//重新設(shè)置文字
weakself.textLabel.text = @"清除緩存(0B)";
});
});
}];
}
6,細(xì)節(jié)處理
在上面我們用到了UIActivityIndicatorView,那么問題來了.當(dāng)我們把tableView上下滑動的時候的,cell的UIActivityIndicatorView會消失不見.這是問什么了?
這是因為動畫效果導(dǎo)致的,解決辦法就是在每次cell出現(xiàn)的時候拿到UIActivityIndicatorView,讓它開始動畫就行了.
//cell每次出現(xiàn)的頁面都會調(diào)用這個方法,取出這個indicatorView刷新UI界面
UIActivityIndicatorView *indicatorView = (UIActivityIndicatorView *)self.accessoryView;
[indicatorView startAnimating];
7,結(jié)束語
這就是我總結(jié)的清理緩存的知識,希望可以幫到大家.如有問題歡迎聯(lián)系我,我會第一時間回答大家的問題.