清除缓存

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"清除缓存" style:(UIBarButtonItemStyleDone) target:self action:@selector(deletClink)];

- (void)deletClink {

//读取缓存大小

NSString *str = [NSString stringWithFormat:@"%.2fM",[self readCacheSize]];

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:str delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];

[alert show];

}

//2. 清除缓存

- (void)clearFile

{

NSString * cachePath = [NSSearchPathForDirectoriesInDomains (NSCachesDirectory , NSUserDomainMask , YES ) firstObject];

NSArray * files = [[NSFileManager defaultManager ] subpathsAtPath :cachePath];

//NSLog ( @"cachpath = %@" , cachePath);

for ( NSString * p in files) {

NSError * error = nil ;

//获取文件全路径

NSString * fileAbsolutePath = [cachePath stringByAppendingPathComponent :p];

if ([[NSFileManager defaultManager ] fileExistsAtPath :fileAbsolutePath]) {

[[NSFileManager defaultManager ] removeItemAtPath :fileAbsolutePath error :&error];

}

}

}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

if (buttonIndex == 1) {

[self clearFile];

}

}

//1. 获取缓存文件的大小

-( float )readCacheSize

{

NSString *cachePath = [NSSearchPathForDirectoriesInDomains (NSCachesDirectory , NSUserDomainMask , YES) firstObject];

return [ self folderSizeAtPath :cachePath];

}

//由于缓存文件存在沙箱中,我们可以通过NSFileManager API来实现对缓存文件大小的计算。

// 遍历文件夹获得文件夹大小,返回多少 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);

}

// 计算 单个文件的大小

- ( long long ) fileSizeAtPath:( NSString *) filePath{

NSFileManager * manager = [NSFileManager defaultManager];

if ([manager fileExistsAtPath :filePath]){

return [[manager attributesOfItemAtPath :filePath error : nil] fileSize];

}

return 0;

}

你可能感兴趣的:(清除缓存)