Cache files stored in the Sandbox folder caches, the implementation of clearing the cache, the main implementation is to find files-delete files (which involves the calculation file size)
The following is the main code that implements the purge cache
Get Cache Path
-(NSString *) getcache{
NSString *cachepath = [Nssearchpathfordirectoriesindomains (nscachesdirectory, Nsuserdomainmask, YES) firstObject];
NSLog (@ "%@", CachePath);
return CachePath;
}
Get folder Size
-(CGFloat) foldersizeatpath{
Creating a File Manager
Nsfilemanager *manager = [Nsfilemanager Defaultmanager];
Get folder path
NSString *folderpath = [self getcache];
Returns 0 if the cache file does not exist
if (![ Manager Fileexistsatpath:folderpath]) {
return 0;
}
Calculate the size of the cache file if the cache file exists
Nsenumerator *childfilesenumerator = [[manager Subpathsatpath:folderpath] objectenumerator];//get enumerator from the file array in the cache path
NSString *filename = nil;//defines an empty file name used to get the file name in the enumerator
Long Long foldersize = 0;//defines and initializes a file size of 0
while ((FileName = [Childfilesenumerator nextobject]) = nil) {
Get the absolute path to a single file
NSString *fileabsolutepath = [FolderPath stringbyappendingpathcomponent:filename];
Define a single file variable and initialize it to 0
float singlefilesize = 0.0;
Gets the size of a single file if the individual file exists
if ([manager Fileexistsatpath:fileabsolutepath]) {
Attributesofitematpath: Get the properties of a file
Singlefilesize = [[Manager Attributesofitematpath:fileabsolutepath Error:nil] fileSize];
}
Foldersize + = Singlefilesize;
}
return foldersize;
}
Clear Cache
-(void) clearcache{
Nsfilemanager *manager = [Nsfilemanager Defaultmanager];
NSString *floderpath = [self getcache];
if ([manager Fileexistsatpath:floderpath]) {
Nsarray *childfiles = [manager Subpathsatpath:floderpath];
For (NSString *filename in childfiles) {
NSString *absolutepath = [Floderpath stringbyappendingpathcomponent:filename];
[Manager Removeitematpath:absolutepath Error:nil];
}
}
[[Sdimagecache Sharedimagecache] cleandisk];//because I used the sdwebimage this third-party class, so with this kind of self-bringing method to implement
}
Clear the implementation code for the cache feature