In app development, a large number of images or messages occupy the system memory, resulting in a heap of spam information, so the development of clear cache function is necessary.
Code Snippet 1: Get the file size
-(longlong) Filesizeatpath: (nsstring*) filepath{ nsfilemanager* manager = [ Nsfilemanager Defaultmanager]; if ([manager Fileexistsatpath:filepath]) { return [[Manager Attributesofitematpath: FilePath Error:nil] fileSize]; } return 0 ;}
Code Snippet 2: Get the file size
//Traverse folder to get folder size, return how many M- (float) Foldersizeatpath: (nsstring*) folderpath{Nsfilemanager* Manager =[Nsfilemanager Defaultmanager]; if(! [Manager Fileexistsatpath:folderpath])return 0; Nsenumerator*childfilesenumerator =[[manager Subpathsatpath:folderpath] objectenumerator]; NSString*FileName; Long LongFoldersize =0; while(FileName = [Childfilesenumerator nextobject])! =Nil) {NSString* Fileabsolutepath =[FolderPath Stringbyappendingpathcomponent:filename]; Foldersize+=[self filesizeatpath:fileabsolutepath]; } returnfoldersize/(1024.0*1024.0);}
Code Snippet 3: Clear Cache Function Method one
1. Clear the first type of cache- (void) Action: (ID) sender{//completely clear the cache the first methodUIButton* Button =Sender; [Button Settitle:@"clean up ."Forstate:uicontrolstatenormal]; Nsarray*paths =nssearchpathfordirectoriesindomains (Nslibrarydirectory, Nsuserdomainmask, YES); NSString*path =[Paths Lastobject]; NSString*STR = [NSString stringWithFormat:@"cache has been cleared%.1FM", [self foldersizeatpath:path]]; NSLog (@"%@", str); Nsarray*files =[[Nsfilemanager Defaultmanager] subpathsatpath:path]; for(NSString *pinchfiles) {Nserror*error; NSString*path =[path stringbyappendingpathcomponent:p]; if([[[Nsfilemanager Defaultmanager] Fileexistsatpath:path]) {[[Nsfilemanager Defaultmanager] RemoveItemAtPat H:path Error:&ERROR]; } }}
Code Snippet 4: Clear Cache Function Method two
[[Sdimagecache Sharedimagecache] cleardisk]; [[Sdimagecache Sharedimagecache] clearmemory];
iOS Clear Cache feature development