您的位置:首页 > 其它

清理沙盒缓存&获取缓存size

2015-09-16 18:06 288 查看

清理沙盒缓存



1、清理当前沙盒中的缓存信息

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:nilwaitUntilDone:YES];});

-(void)clearCacheSuccess
{
NSLog(@"清理成功");
}


2、SDImage第三方清除缓存的方法: (清除的一般是图片的缓存)

[[SDImageCache sharedImageCache] clearDisk];  //清楚磁盘缓存

[[SDImageCache sharedImageCache] clearMemory];    //清楚内存缓存


获取缓存Size



1、获取cache中当前缓存数据的大小

NSString *filenameNavigationBar=[CacheDownMessage getTargetPathWithBasepath:@"listData.plist"];//获取路径
NSDictionary * dictNavigationBar = [[NSFileManager defaultManager] attributesOfItemAtPath:filenameNavigationBar error:nil];
NSString *stringNavigationBarLength = [dictNavigationBar objectForKey:NSFileSize];


2.1、计算文件大小

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

NSFileManager* manager = [NSFileManager defaultManager];

if ([manager fileExistsAtPath:filePath]){

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

}

return 0;

}


2.2、计算文件大小

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

}


获取cache文件夹中文件个数

NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:cachPath];
NSLog(@"files :%d",[files count]);


/article/2153109.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: