码迷,mamicode.com
首页 > 其他好文 > 详细

清除缓存

时间:2015-11-09 15:47:48      阅读:219      评论:0      收藏:0      [点我收藏+]

标签:

清除缓存

#pragma mark 计算单个文件
- (float)fileSizeAtPath:(NSString*)path {

NSFileManager*fileManager = [NSFileManager defaultManager];

if ([fileManager fileExistsAtPath:path]) {

longlongsize = [fileManager attributesOfItemAtPath:path

error:nil].fileSize;
NSLog(@"%f",size /1024.0/

1024.0);
returnsize /1024.0/1024.0;

}
return 0;

}

#pragma mark 计算目录大小
- (float)folderSizeAtPath:(NSString*)path {

NSFileManager*fileManager = [NSFileManager defaultManager];

float folderSize;

if ([fileManager fileExistsAtPath:path]) {

NSArray *childerFiles = [fileManager subpathsAtPath:path];

for (NSString *fileName in childerFiles) {

NSString*absolutePath = [path stringByAppendingPathComponent:fileName];

folderSize += [self fileSizeAtPath:absolutePath];

}

//SDwebImage框架自身计算缓存的实现 folderSize+= [[SDImageCache

sharedImageCache]getSize] /1024.0/1024.0; NSLog(@"------%f",folderSize);

return folderSize;

}
return 0;

}

#pragma mark 清除缓存 - (void)clear

{
dispatch_async(

dispatch_get_global_que ue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)

, ^{

NSString*cachPath = [NSSearchPathForDirectoriesInDomains(NSCac

hesDirectory,NSUserDomainMask, YES) objectAtIndex:0];

NSArray*files = [[NSFileManager defaultManager]

subpathsAtPath:cachPath];

NSLog(@"files :%lu" ,(unsigned long)[files count]);

for (NSString *p in

NSError *error;

NSString*path = stringByAppendingPathComponent:p];

if

([[NSFileManager defaultManager] fileExistsAtPath:path]) {

[[NSFileMan ager defaultManager] removeItemAtPath:path

error:&error];

} }

[self performSelectorOnMainThread:@selector(clea

rCacheSuccess) withObject:nil waitUntilDone:YES];});
}

-(void)clearCacheSuccess {

NSLog(@"清除缓存");

files) {

[cachPath

清除缓存

标签:

原文地址:http://www.cnblogs.com/lidongxiao/p/4949985.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!