iOS中的缓存计算和清除
2017-07-20 16:35
323 查看
<1>怎么计算缓存大小(主要是利用系统提供的NSFileManager类来实现)
1.单个文件大小的计算-(long long)fileSizeAtPath:(NSString *)path{ NSFileManager *fileManager=[NSFileManager defaultManager]; if([fileManager fileExistsAtPath:path]){ long long size=[fileManager attributesOfItemAtPath:path error:nil].fileSize; return size; } return 0; }
2.文件夹大小的计算(要利用上面的1提供的方法
-(float)folderSizeAtPath:(NSString *)path{ NSFileManager *fileManager=[NSFileManager defaultManager]; NSString *cachePath=[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject]; cachePath=[cachePath stringByAppendingPathComponent:path]; long long folderSize=0; if ([fileManager fileExistsAtPath:cachePath]) { NSArray *childerFiles=[fileManager subpathsAtPath:cachePath]; for (NSString *fileName in childerFiles) { NSString *fileAbsolutePath=[cachePath stringByAppendingPathComponent:fileName]; long long size=[self fileSizeAtPath:fileAbsolutePath]; folderSize += size; NSLog(@"fileAbsolutePath=%@",fileAbsolutePath); } //SDWebImage框架自身计算缓存的实现 folderSize+=[[SDImageCache sharedImageCache] getSize]; return folderSize/1024.0/1024.0; } return 0; }
其中folderSize+=[[SDImageCache sharedImageCache] getSize];这行代码是SDWebImage给我们提供的计算本地缓存图片大小的方法….(当然了,这个方法的底层实现依然是用的NSFileManager做的)
上面2个方法结合起来使用,就可以计算我们总共产生多少缓存啦….2.计算好了缓存,那么怎么清除呢??
//同样也是利用NSFileManager API进行文件操作,SDWebImage框架自己实现了清理缓存操作,我们可以直接调用
//同样也是利用NSFileManager API进行文件操作,SDWebImage框架自己实现了清理缓存操作,我们可以直接调用。 -(void)clearCache:(NSString *)path{ NSString *cachePath=[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject]; cachePath=[cachePath stringByAppendingPathComponent:path]; NSFileManager *fileManager=[NSFileManager defaultManager]; if ([fileManager fileExistsAtPath:cachePath]) { NSArray *childerFiles=[fileManager subpathsAtPath:cachePath]; for (NSString *fileName in childerFiles) { //如有需要,加入条件,过滤掉不想删除的文件 NSString *fileAbsolutePath=[cachePath stringByAppendingPathComponent:fileName]; NSLog(@"fileAbsolutePath=%@",fileAbsolutePath); [fileManager removeItemAtPath:fileAbsolutePath error:nil]; } } [[SDImageCache sharedImageCache] cleanDisk]; }
相关文章推荐
- IOS 之 计算缓存,清除缓存
- iOS 文件管理器(文件操作, 缓存计算, 清除缓存)
- ios开发计算缓存的大小以及清除
- iOS中的缓存计算和清除完整实例代码
- iOS计算缓存大小、清除缓存
- ioS 计算缓存和清除缓存
- IOS开发——ASIDownloadCache计算缓存大小&清除缓存
- iOS文件管理,文件夹目录调取+缓存读取计算清除
- iOS 缓存的获取计算与清除归零
- iOS计算缓存文件大小, 和清除缓存功能
- iOS中的缓存计算和清除(二)
- iOS计算缓存大小、清除缓存
- iOS SDwebimage清除缓存
- iOS清除缓存详细解析、app文件路径操作,自己个人对于沙盒的理解
- ios开发清除SDWebImage图片缓存
- iOS_清除xcode编译器的缓存
- iOS 计算SDWebImage本地缓存图片大小+清理本地缓存的图片
- iOS 计算缓存大小以及清理缓存
- iOS UIWebView清除缓存
- 计算缓存与清除缓存