Mobile applications in processing network resources, generally do offline cache processing, which is the most typical image caching, which is very popular offline caching framework for sdwebimage.
However, offline caching consumes cell phone storage space, so caching cleanup is basically the standard feature of information, shopping, and reading apps.
The implementation of off-line caching, which is introduced today, is mainly divided into the acquisition of cache file size and the realization of clearing cache file.
1. Get the size of the cached file
-(float) readcachesize
{
nsstring *cachepath = [Nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES) firstobject];
return [self Foldersizeatpath:cachepath];
}
Because cached files are in the sandbox, we can use the Nsfilemanager API to compute the size of the cached file.
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 long foldersize = 0;
while (fileName = [Childfilesenumerator nextobject])!= nil) {
//Get file Full path
nsstring * Fileabsolutepath = [folderpa Th stringbyappendingpathcomponent:filename];
Foldersize + = [self Filesizeatpath:fileabsolutepath];
}
Return foldersize/(1024.0 * 1024.0);
}
Calculates the size of a single file
-(Long Long) Filesizeatpath: ( NSString *) filepath{
Nsfilemanager * manager = [Nsfilemanager Defaultmanager];
if ([manager Fileexistsatpath:filepath]) {return
[[manager Attributesofitematpath:filepath Error:nil] FileSize] ;
}
return 0;
}
2. Clear Cache
-(void) clearfile
{
NSString * CachePath = [Nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES) firstobject];
Nsarray * files = [[Nsfilemanager Defaultmanager] subpathsatpath:cachepath];
NSLog (@ "Cachpath =%@", CachePath);
For (NSString * p in files) {
Nserror * error = nil;
Get file Full path
nsstring * Fileabsolutepath = [CachePath stringbyappendingpathcomponent:p];
if ([[[Nsfilemanager Defaultmanager] Fileexistsatpath:fileabsolutepath]) {
[[Nsfilemanager Defaultmanager] Removeitematpath:fileabsolutepath error:&error];
}
}
Read cache size
float cacheSize = [self readcachesize] *1024;
Self.cacheSize.text = [NSString stringwithformat:@ "%.2fkb", cacheSize];
}
The above is a small set of iOS for you to get the size of the cache file and clear the cache file 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!