IOS gets the size of the cached file and clears the cached file method _ios

Source: Internet
Author: User

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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.