Cache occupies a lot of space in the system, how real-time dynamic display of the size of the cache, so that users clearly understand the accumulation of the cache, an effective key to clean up it?
For the convenience of readers and the future themselves better understand, we create such scenes. (Create a Uilabel in the table view's purge cache cell *cachelabel to display the current cache, click OK, clear the cache when clicking the cell pop-up Prompt box.)
Here is the implementation code:
#pragma mark-Compute cache Size-(NSString *) getcachesize {//define variable store total cache size Long Long sumsize = 0; Get the current picture cache path NSString *cachef
Ilepath = [Nshomedirectory () stringbyappendingpathcomponent:@ "library/caches"];
02. Create File Management Objects Nsfilemanager *filemanager = [Nsfilemanager Defaultmanager];
Gets all the subpath under the current cache path Nsarray *subpaths = [FileManager subpathsofdirectoryatpath:cachefilepath error:nil]; Iterates through all the child files for (NSString *subpath in subpaths) {//1). Stitching full path NSString *filepath = [Cachefilepath stringbyappendingformat:@]
/%@ ", subpath];
2. The size of the computed file is long fileSize = [[FileManager attributesofitematpath:filepath error:nil]filesize];
3. Load to file size sumsize + = fileSize;
float size_m = sumsize/(1000*1000);
return [NSString stringwithformat:@ "%.2FM", size_m]; #pragma mark-Clear cache Hint (uitableviewdatasourcedelegate)-(void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) Indexpath {if (Indexpath.row = = 0) {Uialertview *alertview = [[UIAlertView alloc] initwithtitle:@ Cache Clear "message:@" determines cleanup slow"Delegate:self cancelbuttontitle:@" Cancel "otherbuttontitles:@" OK ", nil];
[Alertview show]; #pragma mark-uialertviewdelegate method Implementation-(void) Alertview: (Uialertview *) Alertview Clickedbuttonatindex: (Nsinteger)
Buttonindex {NSLog (@ "code execution to this");//Judge clicks the confirmation key if (Buttonindex = 1) {//01 ...
Nsfilemanager *filemanager = [Nsfilemanager Defaultmanager];
02..
NSString *cachefilepath = [Nshomedirectory () stringbyappendingpathcomponent:@ "library/caches"];
03. ...
[FileManager Removeitematpath:cachefilepath Error:nil];
04 Refresh the first row of cells nsindexpath *indexpath = [Nsindexpath indexpathforitem:0 insection:0];
[_tableview Reloadrowsatindexpaths:@[indexpath] withrowanimation:uitableviewrowanimationfade]; 05:04 and 05 Use one can [_tableview reloaddata];//refresh table View} @pragma-mark- Placed in the first paragraph of the. m file is more appropriate, this demo only do functional display, real-time monitoring cache size, from other interfaces to the page, you also need to refresh the following table View-(void) Viewwillappear: (BOOL) animated {[Super
Viewwillappear:yes];
[_tableview Reloaddata]; }
The above is a small series to introduce the iOS cache file size display function and a key cleaning function of the implementation method, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!