絕大多數應用中都存在著清楚緩存的功能,形形色色,各有千秋,現為大家介紹一種最基礎的清除緩存的方法。清除緩存基本上都是在設置界面的某一個cell,于是我們可以把清除緩存封裝在某一個自定義cell中,如下圖所示:
具體步驟
使用注意:過程中需要用到第三方庫,請提前安裝好:sdwebimage、svprogresshud。
1. 創建自定義cell,命名為gylclearcachecell
重寫initwithstyle:(uitableviewcellstyle)style reuseidentifier:(nsstring *)reuseidentifier方法,設置基本內容,如文字等等;主要代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
- (instancetype)initwithstyle:(uitableviewcellstyle)style reuseidentifier:(nsstring *)reuseidentifier { if (self = [super initwithstyle:style reuseidentifier:reuseidentifier]) { // 設置加載視圖 uiactivityindicatorview *loadingview = [[uiactivityindicatorview alloc] initwithactivityindicatorstyle:uiactivityindicatorviewstylegray]; [loadingview startanimating]; self.accessoryview = loadingview; //設置文字 self.textlabel.text = @ "清楚緩存" ; self.detailtextlabel.text = @ "正在計算" ; } return self; } |
2. 計算緩存文件大小
緩存文件包括兩部分,一部分是使用sdwebimage緩存的內容,其次可能存在自定義的文件夾中的內容(視頻,音頻等內容),于是計算要分兩部分,主要代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
unsigned long long size = [nssearchpathfordirectoriesindomains(nscachesdirectory, nsuserdomainmask, yes).lastobject stringbyappendingpathcomponent:@ "customfile" ].filesize; //filesize是封裝在category中的。 size += [sdimagecache sharedimagecache].getsize; //customfile + sdwebimage 緩存 //設置文件大小格式 nsstring sizetext = nil; if (size >= pow (10, 9)) { sizetext = [nsstring stringwithformat:@ "%.2fgb" , size / pow (10, 9)]; } else if (size >= pow (10, 6)) { sizetext = [nsstring stringwithformat:@ "%.2fmb" , size / pow (10, 6)]; } else if (size >= pow (10, 3)) { sizetext = [nsstring stringwithformat:@ "%.2fkb" , size / pow (10, 3)]; } else { sizetext = [nsstring stringwithformat:@ "%zdb" , size]; } |
上述兩個方法都是在主線程中完成的,如果緩存文件大小非常大的話,計算時間會比較長,會導致應用卡死,考慮到該問題,因此需要將上述代碼放到子線程中完成。
3. 添加手勢監聽
對于監聽點擊cell可以使用代理也可以使用手勢監聽,為了將完整的功能封裝到自定義cell中,于是我們使用手勢監聽的方法來監聽點擊cell。
1
2
3
4
5
6
7
|
//計算完成后,回到主線程繼續處理,顯示文件大小,除去加載視圖,顯示箭頭,添加點擊事件 dispatch_async(dispatch_get_main_queue(), ^{ self.detailtextlabel.text = [nsstring stringwithformat:@ "%@" ,sizetext]; self.accessoryview = nil; self.accessorytype = uitableviewcellaccessorydisclosureindicator; [self addgesturerecognizer:[[uitapgesturerecognizer alloc] initwithtarget:self action:@selector(clearcacheclick)]]; }); |
4. 清除緩存
清除緩存也是分為兩部分,一是清除sdwebimage的緩存,二是清除自定義文件緩存,主要代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
- ( void )clearcacheclick { [svprogresshud showwithstatus:@ "正在清除緩存···" ]; [svprogresshud setdefaultmasktype:svprogresshudmasktypeblack]; [[sdimagecache sharedimagecache] cleardiskoncompletion:^{ dispatch_async(dispatch_get_global_queue(0, 0), ^{ nsfilemanager *mgr = [nsfilemanager defaultmanager]; [mgr removeitematpath:gylcustomfile error:nil]; [mgr createdirectoryatpath:gylcustomfile withintermediatedirectories:yes attributes:nil error:nil]; dispatch_async(dispatch_get_main_queue(), ^{ [svprogresshud dismiss]; // 設置文字 self.detailtextlabel.text = nil; }); }); }]; } |
注意點:sdwebimage清除緩存是在子線程中進行的,清除自定義文件內容應該也放在子線程中(刪除大文件可能比較耗時),為了保證兩者不沖突,可以將刪除自定義文件內容放在sdwebimage緩存清除完畢之后進行,然后再回到主線程操作。
5. 其他注意點
a. 在計算文件大小過程中應該是不允許點擊cell的,如果有設置cell的didselectrowatindexpath方法,那么會導致手勢監聽不能使用。于是需要在計算時不能點擊cell。
b. 設置userinteractionenabled=no應放在設置文字之后,否則textlabel將顯示為灰色。
c. 當計算文件大小沒有結束的時,這個時候點擊返回,自定義cell不會被銷毀,他會執行完剩下的代碼,可以使用dealloc方法來驗證,在此情況下,可以使用弱引用的self來解決。
d. 當設置界面的cell比較多時,如果還在計算緩存大小時,清除緩存的cell從視圖中消失,那么加載視圖動畫就會被停止,當返回到清除緩存cell時,看不到加載動畫。解決方案兩種方法:一個是在cell創建的代理方法中重新開啟動畫;另一個是封裝到layoutsubviews方法中。
6. 使用
創建gylsettingviewcontroller繼承自uitableviewcontroller;首先為自定義cell注冊;其次在數據源方法中使用自定義cell;具體代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
#import "gylsettingviewcontroller.h" #import "gylclearcachecell.h" @implementation gylsettingviewcontroller static nsstring * const gylclearcachecellid = @ "clearcache" ; static nsstring * const gylsettingcellid = @ "setting" ; - (instancetype)init { return [self initwithstyle:uitableviewstylegrouped]; } - ( void )viewdidload { [super viewdidload]; self.view.backgroundcolor = gylbgcolor; self.navigationitem.title = @ "設置" ; [self.tableview registerclass:[gylclearcachecell class ] forcellreuseidentifier:gylclearcachecellid]; [self.tableview registerclass:[uitableviewcell class ] forcellreuseidentifier:gylsettingcellid]; } - (nsinteger)numberofsectionsintableview:(uitableview *)tableview { return 3; } - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { return 1; } - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { if (indexpath.section == 0 && indexpath.row == 0) { return [[gylclearcachecell alloc] initwithstyle:uitableviewcellstylevalue1 reuseidentifier:gylclearcachecellid]; } uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:gylsettingcellid]; cell.textlabel.text = [nsstring stringwithformat:@ "section-%zd,row--%zd" ,indexpath.section,indexpath.row]; return cell; } @end |
7. 效果
計算文件大小
正在清除緩存
清除完畢
總結
以上所述是小編給大家介紹的ios中設置清除緩存功能的實現方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!
原文鏈接:http://blog.csdn.net/wanglei0918/article/details/75557412