The iOS application can only read files in the file system created for the program, and it cannot be accessed anywhere else, and this area becomes a sandbox . It consists of three folders:
Documents : Apple recommends that file data that is established in the program or browsed in the program be kept in this directory, and that itunes backup and restore will include this directory, such as user information, such as permanent files;
Library: It contains two folders caches and preferences
library/caches: store cache files, itunes does not back up this directory, files in this directory will not be deleted in the app exit, slice, video cache;
library/preferences: contains the application's preferences file;
Temp: This directory is used to store temporary files, saving information that is not needed during the application restart.
Here's how to get the sandbox path:
1,//The root directory of the sandbox nsstring *homepath = Nshomedirectory ();
2,///Sandbox documents path nsstring *docupath = [Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) lastobject];
3,//sandbox in the library path nsstring *libpath = [Nssearchpathfordirectoriesindomains (Nslibrarydirectory, Nsuserdomainmask , YES) lastobject];
< Span class= "hljs-literal" >< Span class= "Hljs-comment" > 4,//sandbox library/caches path nsstring *cachespath = [ Nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES) Lastobject];
5,///Sandbox temp path nsstring *temppath = Nstemporarydirectory ();
Calculate the size of a folder
-(float) Calculatefoldersizewithpath: (NSString *) path{
Nsfilemanager *filemanager=[nsfilemanager Defaultmanager];
NSString *cachepath=[nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES) firstObject];
Cachepath=[cachepath Stringbyappendingpathcomponent:path];
CGFloat foldersize=0;
if ([FileManager Fileexistsatpath:cachepath])
{
Nsarray *childfiles=[filemanager Subpathsatpath:cachepath];
For (NSString *filename in Childfiles)
{
NSString *fileabsolutepath=[cachepath Stringbyappendingpathcomponent:filename];
CGFloat size=[self Calculatefilesizewithpath:fileabsolutepath];
Foldersize + = size;
NSLog (@ "fileabsolutepath=%@", Fileabsolutepath);
}
Sdwebimage The framework itself calculates the cache
Foldersize+=[[sdimagecache Sharedimagecache] getsize];
return foldersize/1024.0/1024.0;
}
return 0;
}
Calculate a single File size
-(CGFloat) Calculatefilesizewithpath: (NSString *) path{
Nsfilemanager *filemanager=[nsfilemanager Defaultmanager];
if ([FileManager Fileexistsatpath:path]) {
CGFloat Size=[filemanager Attributesofitematpath:path error:nil].filesize;
return size;
}
return 0;
}
-(void) Clearcacheclick
{
Nsarray *arr_path = @[@ "/media", @ "/music", @ "/voice"];
[[UIView alloc]showhudwithtitle:@ is clearing the cache ...] WITHSTATE:1];
One is to clear the sdwebimage cache, and the second is to clear the custom file cache
[[Sdimagecache Sharedimagecache] cleardiskoncompletion:^{
Dispatch_async (dispatch_get_global_queue (0, 0), ^{
NSString *documentdir = [Nssearchpathfordirectoriesindomains (nsdocumentdirectory, Nsuserdomainmask, YES) OBJECTATINDEX:0];
For (NSString *pathname in Arr_path) {
NSString *fileabsolutepath = [Documentdir stringbyappendingpathcomponent:pathname];
Nsfilemanager *filemanager = [Nsfilemanager Defaultmanager];
if ([FileManager Fileexistsatpath:fileabsolutepath]) {
[FileManager Removeitematpath:fileabsolutepath Error:nil];
[FileManager Createdirectoryatpath:fileabsolutepath withintermediatedirectories:yes Attributes:nil Error:nil];
}
}
Dispatch_async (Dispatch_get_main_queue (), ^{
[[UIView alloc]showandhidehudwithtitle:@] Clear complete "withstate:0";
Set text
Self.lable_Cache.text = @ "0.00 MB";
});
});
}];
}
IOS Clear Cache