标签:
清除缓存
#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