(117) Basic file Operation-sdwebimage Clear Cache-size calculation of folders

Source: Internet
Author: User
Tags tmp folder

In the iOS app sandbox, documents and Library/preferences are backed up to icloud, so it's only appropriate to place some record files, such as plist, database files. Caches generally placed in the Library/caches,tmp folder are randomly cleared by the system and are not suitable for preventing data.

"Erase of picture cache"

When using Sdwebimage, pictures are heavily cached and sometimes need to get the size of the cache and clear the cache.

To get the cache size, use the GetSize method of the Sdimagecache singleton to get the cache size in bytes, noting that calculations are calculated by 1k=1000.

The method to get the file size of M is as follows:

Double size = [[Sdimagecache Sharedimagecache] getsize]/1000.0/1000.0;

To clear the cache, call the Cleardisk method, which is divided into callbacks and no callbacks.

Because the time to clear the cache may be longer, it should be indicated with an indicator.

[[Sdimagecache Sharedimagecache] cleardiskoncompletion:^{           //cleared processing.        }];

"Folder size calculation"

Using Nsfilemanager can get the properties of the file, if the file is a directory, get the filesize is meaningless, because the size of the directory needs recursive calculation, not as a static property. Therefore, only the file's FileSize property is the file size.

In order to calculate the size of the folder, should be recursive internal all files, fortunately, Apple officially integrated recursive method, through recursion can get all the directory and all the files, as long as the use of FileManager method to determine whether it is a file, if it is a file to get filesize attribute accumulation, You can calculate the size of the folder, as follows:

It is important to note that the files that are traversed are rooted in caches, so you should stitch the full path when you get the attributes.

-(void) filesize{    nsfilemanager *manager = [Nsfilemanager Defaultmanager];        NSString *cachepath = [Nssearchpathfordirectoriesindomains (nscachesdirectory, Nsuserdomainmask, YES) lastObject];        Nsarray *files = [manager Subpathsofdirectoryatpath:cachepath Error:nil]; Recursively all sub-paths        nsinteger totalsize = 0;        For (NSString *filepath in files) {        NSString *path = [CachePath Stringbyappendingpathcomponent:filepath];        Determine if the file is        BOOL isdir = NO;        [Manager Fileexistsatpath:path isdirectory:&isdir];        if (!isdir) {            nsdictionary *attrs = [manager Attributesofitematpath:path Error:nil];            TotalSize + = [attrs[nsfilesize] integervalue];}    }        NSLog (@ "%d", totalsize);    }



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

(117) Basic file Operations-sdwebimage Clear Cache-size calculation of folders

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.