0122---Clean the cache

Source: Internet
Author: User

iOS cleans up the cache in several ways, with the need for friends to refer to below.




-(Long Long) Filesizeatpath: (nsstring*) filepath{

nsfilemanager* manager = [Nsfilemanager Defaultmanager];

if ([manager Fileexistsatpath:filepath]) {

return [[Manager Attributesofitematpath:filepath Error:nil] fileSize];

}

return 0;

}

TraverseFolder to get the 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) {

nsstring* Fileabsolutepath = [FolderPath stringbyappendingpathcomponent:filename];

Foldersize + = [self filesizeatpath:fileabsolutepath];

}

Return foldersize/(1024.0*1024.0);

}

1. Clear the first type of cache

-(void) Action: (ID) sender

{

Completely clear the cache the first method

UIButton * button = sender;

[Button settitle:@ "clean up" forstate:uicontrolstatenormal];

Nsarray *paths = Nssearchpathfordirectoriesindomains (Nslibrarydirectory, Nsuserdomainmask, YES);

NSString *path = [Paths lastobject];



NSString *str = [nsstring stringwithformat:@ "cache cleared%.1FM", [self foldersizeatpath:path]];

NSLog (@ "%@", str);

Nsarray *files = [[Nsfilemanager Defaultmanager] subpathsatpath:path];

For (NSString *p in files) {

Nserror *error;

NSString *path = [Path stringbyappendingpathcomponent:p];

if ([[[Nsfilemanager Defaultmanager] Fileexistsatpath:path]) {

[[Nsfilemanager Defaultmanager] Removeitematpath:path error:&error];

}

}



2. Sdimage third-party methods to clear the cache

[[Sdimagecache Sharedimagecache] cleardisk];

[[Sdimagecache Sharedimagecache] clearmemory];



3. Completely clear the cache the second method

Dispatch_async (

Dispatch_get_global_queue (dispatch_queue_priority_default, 0)

, ^{

NSString *cachpath = [Nssearchpathfordirectoriesindomains (nscachesdirectory, Nsuserdomainmask, YES) objectAtIndex:0] ;

NSLog (@ "%@", Cachpath);



Nsarray *files = [[Nsfilemanager Defaultmanager] subpathsatpath:cachpath];

NSLog (@ "Files:%d", [Files Count]);

For (NSString *p in files) {

Nserror *error;

NSString *path = [Cachpath stringbyappendingpathcomponent:p];

if ([[[Nsfilemanager Defaultmanager] Fileexistsatpath:path]) {

[[Nsfilemanager Defaultmanager] Removeitematpath:path error:&error];

}

}

[Self Performselectoronmainthread: @selector (clearcachesuccess) Withobject:nil waituntildone:yes];});

}

-(void) clearcachesuccess

{

NSLog (@ "clean success");

}

0122---Clean the cache

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.