我们在做项目开发的时候,经常会遇到设置清理缓存的问题,对于新手经常会考虑到什么数据存储啊,内存之类一大堆无用的想法,其实清理工程的缓存是一件非常简单的事情。
不用多说:见代码,立即大悟:
@property ( nonatomic , strong ) UILabel * cachLabel;//显示缓存有多少m
最后节目显示:
cell. cachLabel . text = [ NSString stringWithFormat : @"(%.2fM)" , [ self filePath ]];
/"直接复杂下面代码就可以用"/正式开始了哦:============
//1:首先我们计算一下 单个文件的大小
- ( long long ) fileSizeAtPath:( NSString *) filePath{
NSFileManager * manager = [ NSFileManager defaultManager ];
if ([manager fileExistsAtPath :filePath]){
return [[manager attributesOfItemAtPath :filePath error : nil ] fileSize ];
}
return 0 ;
}
//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 );
}
// 显示缓存大小
- ( float )filePath
{
NSString * cachPath = [ NSSearchPathForDirectoriesInDomains ( NSCachesDirectory , NSUserDomainMask , YES ) firstObject ];
return [ self folderSizeAtPath :cachPath];
}
// 清理缓存
- ( void )clearFile
{
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 ];//清理完之后重新导入数据
}
//=====================另外一种写法更简单============
清理缓存的代码如下:
dispatch_async (
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0 )
原文 http://blog.csdn.net/sammyieveo/article/details/41513865
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 ( @" 清理成功 " );
}