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