Mobile Internet app development, must always pay attention to the user experience, so as to avoid the app or mobile phone and other mobile device card death, the following is the processing of cached files.
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, NSUserDomainM
Ask, YES) firstobject];
return [self foldersizeatpath:cachepath];
Because the cache file is in a sandbox, we can compute the size of the cached file through the Nsfilemanager API. 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 = [Folderpat
H 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];
}
Thank you for reading, I hope to help you, thank you for your support for this site!