一、计算缓存
// 缓存大小
- (CGFloat)folderSize{
CGFloat folderSize;
//获取路径
NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES)firstObject];
//获取所有文件的数组
NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:cachePath];
NSLog(@"文件数:%ld",files.count);
for(NSString *path in files) {
NSString*filePath = [cachePath stringByAppendingString:[NSString stringWithFormat:@"/%@",path]];
//累加
folderSize += [[NSFileManager defaultManager]attributesOfItemAtPath:filePath error:nil].fileSize;
}
//转换为M为单位
CGFloat sizeM = folderSize /1024.0/1024.0;
return sizeM;
}
二、清除缓存
- (void)removeCache{
//===============清除缓存==============
//获取路径
NSString*cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES)objectAtIndex:0];
//返回路径中的文件数组
NSArray*files = [[NSFileManager defaultManager]subpathsAtPath:cachePath];
NSLog(@"文件数:%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(@"清除成功");
//这里发送一个通知给外界,外界接收通知,可以做一些操作(比如UIAlertViewController)
}else{
NSLog(@"清除失败");
}
}
}
}
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。