// 显示缓存大小
//NSDocumentDirectory Document目录中的数据
//NSCachesDirectory 缓存中的数据
- ( float )filePath { //NSDocumentDirectory Document目录中的数据 //NSCachesDirectory 缓存中的数据 NSString * cachPath = [ NSSearchPathForDirectoriesInDomains ( NSCachesDirectory , NSUserDomainMask , YES ) firstObject ]; UIAlertView * alertView = [[ UIAlertView alloc ] initWithTitle : @" 提示 " message : [NSString stringWithFormat:@"%f",[ self folderSizeAtPath :cachPath]] delegate : nil cancelButtonTitle : @" 确定 " otherButtonTitles : nil ]; [alertView show ]; return [ self folderSizeAtPath :cachPath]; }
//2: 遍历文件夹获得文件夹大小,返回多少 M(提示:你可以在工程界设置()m)
//2: 遍历文件夹获得文件夹大小,返回多少 M(提示:你可以在工程界设置()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 ); }
//3:首先我们计算一下 单个文件的大小
- (long ) fileSizeAtPath:( NSString *) filePath{ NSFileManager * manager = [ NSFileManager defaultManager ]; if ([manager fileExistsAtPath :filePath]){ return [[manager attributesOfItemAtPath :filePath error : nil ] fileSize ]; } return 0 ; }
// 清理缓存 方法一
- ( void )clearFile { //NSDocumentDirectory Document目录中的数据 //NSCachesDirectory 缓存中的数据 NSString * cachPath = [ NSSearchPathForDirectoriesInDomains ( NSCachesDirectory , NSUserDomainMask , YES ) firstObject ]; NSArray * files = [[ NSFileManager defaultManager ] subpathsAtPath :cachPath]; NSLog ( @"cachpath = %@" , cachPath); for ( NSString * p in files) { NSError * error = nil ; NSString * path = [cachPath stringByAppendingPathComponent :p]; if ([[ NSFileManager defaultManager ] fileExistsAtPath :path]) { [[ NSFileManager defaultManager ] removeItemAtPath :path error :&error]; } } [ self performSelectorOnMainThread : @selector (clearCachSuccess) withObject : nil waitUntilDone : YES ]; } - ( void )clearCachSuccess { NSLog ( @" 清理成功 " ); UIAlertView * alertView = [[ UIAlertView alloc ] initWithTitle : @" 提示 " message : @" 缓存清理完毕 " delegate : nil cancelButtonTitle : @" 确定 " otherButtonTitles : nil ]; [alertView show ]; //[ _tableView reloadData ];//清理完之后重新导入数据 }
方法二
//清除缓存 - (void)clear { dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) , ^{ NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES) objectAtIndex:0]; 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(@"清除缓存"); }
时间: 2024-11-04 14:05:43