一芍躏、計算緩存
// 緩存大小
- (CGFloat)folderSize{
CGFloat folderSize;
//獲取路徑
NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES)firstObject];
//獲取所有文件的數(shù)組
NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:cachePath];
NSLog(@"文件數(shù):%ld",files.count);
for(NSString *path in files) {
NSString*filePath = [cachePath stringByAppendingString:[NSString stringWithFormat:@"/%@",path]];
//累加
folderSize += [[NSFileManager defaultManager]attributesOfItemAtPath:filePath error:nil].fileSize;
}
//轉(zhuǎn)換為M為單位
CGFloat sizeM = folderSize /1024.0/1024.0;
return sizeM;
}
二固灵、清除緩存
- (void)removeCache{
//===============清除緩存==============
//獲取路徑
NSString*cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES)objectAtIndex:0];
//返回路徑中的文件數(shù)組
NSArray*files = [[NSFileManager defaultManager]subpathsAtPath:cachePath];
NSLog(@"文件數(shù):%ld",[files count]);
for(NSString *p in files){
NSError*error;
NSString*path = [cachePath stringByAppendingString:[NSString stringWithFormat:@"/%@",p]];
if([[NSFileManager defaultManager]fileExistsAtPath:path])
{
BOOL isRemove = [[NSFileManager defaultManager]removeItemAtPath:path error:&error];
if(isRemove) {
NSLog(@"清除成功");
//這里發(fā)送一個通知給外界,外界接收通知轴术,可以做一些操作(比如UIAlertViewController)
}else{
NSLog(@"清除失敗");
}
}
}
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者