#import "ViewController.h"
@interface viewcontroller ()
@end
@implementation Viewcontroller
-(void) viewdidload {
[Super viewdidload];
//
NSLog (@ " cache file size is %@", [NSString stringwithformat:@ "%0.2fm", [Self foldersizeatpath:[nsstring stringwithformat:@ "%@/library/caches/imagecache", Nshomedirectory ()]]);
#pragma mark sandbox:Library ( system files ) Temp ( temp file ) documents ( for App cache )
NSLog(@ "===%@===", [nsstring stringwithformat:@ "%0.2f", [ Self foldersizeatpath:@ "/users/liushuo/desktop/1567pro "]);
// Clear Cache
[self clearcachefrompath:@ "/users/liushuo/desktop/on- line related documents "];
// Here is the directory that corresponds to the cached image of sdwebimage
// can be changed according to the corresponding requirements of the corresponding file directory
additional setup after loading the view, typically from a nib.
}
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;
}
Traverse folder to get folder size, return how many M
-(float ) Foldersizeatpath: (nsstring*) folderpath{
Nsfilemanager* manager = [nsfilemanager defaultmanager];
If (![ Manager Fileexistsatpath: FolderPath])
return 0;
// traverse all files in a folder through enumeration traversal
// Create an enumeration walker
nsenumerator *childfilesenumerator = [[Manager Subpathsatpath: folderpath] objectenumerator] ;
// Declare file name, file size first
nsstring* fileName;
long long foldersize = 0;
while ((fileName = [Childfilesenumerator nextobject]) = nil) {
// Get the path of the current traversal file
nsstring* Fileabsolutepath = [FolderPath stringbyappendingpathcomponent: fileName];
// Call the encapsulated method to get a single file size
Foldersize + = [self filesizeatpath: Fileabsolutepath];
}
return foldersize/(1024.0*1024.0); convert to how many M to return
}
#pragma mark clears the cache size print nshomedritiony go to Documents to view the path
-(void) Clearcachefrompath: (nsstring*) path{
// Create a File Manager
Nsfilemanager * manager = [nsfilemanager defaultmanager];
if ([manager Fileexistsatpath:p Ath]) {
// If the file path exists to get all of the files
Nsarray * Filearr = [manager subpathsatpath:p Ath]; Locate the path to all the child files and save them in the array.
// need to convert to full path first
// Delete all sub-files directly
for (int i = 0; i < Filearr. Count; i++) {
nsstring * fileName = filearr[i];
// full path
nsstring * filePath = [path stringbyappendingpathcomponent: fileName];
NSLog(@ "%@", FilePath);
[Manager removeItemAtPath: FilePath error:nil];
}
}
}
-(void) didreceivememorywarning {
[Super didreceivememorywarning];
//Dispose of any resources, can be recreated.
}
@end
Clear the cache, calculate the folder size