iOS学习之清除SDWebImage产生的缓存
使用SDWebImage 请求图片数据,会产生相应的缓存
//这是用于显示缓存大小
调用方法:[self filePath]; 即可获得当前缓存大小
// 显示缓存大小
- ( float )filePath
{
NSString * cachPath = [ NSSearchPathForDirectoriesInDomains ( NSCachesDirectory , NSUserDomainMask , YES ) firstObject ];
return [ self folderSizeAtPath :cachPath];
}
//1:首先我们计算一下 单个文件的大小
- ( long long ) fileSizeAtPath:( NSString *) filePath{
NSFileManager * manager = [ NSFileManager defaultManager ];
if ([manager fileExistsAtPath :filePath]){
return [[manager attributesOfItemAtPath :filePath error : nil ] fileSize ];
}
return 0 ;
}
//2: 遍历文件夹获得文件夹大小,返回多少 M(提示:你可以在工程界设置()m)
- ( float ) folderSizeAtPath:( NSString *) folderPath{
NSFileManager * manager = [ NSFileManager defaultManager ];
if (![manager fileExistsAtPath :folderPath]) return 0 ;
NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath :folderPath] objectEnumerator ];
NSString * fileName;
long long folderSize = 0 ;
while ((fileName = [childFilesEnumerator nextObject ]) != nil ){
NSString * fileAbsolutePath = [folderPath stringByAppendingPathComponent :fileName];
folderSize += [ self fileSizeAtPath :fileAbsolutePath];
}
return folderSize/( 1024.0 * 1024.0 );
}
//清理缓存调用的方法
//清理缓存
[[SDImageCache sharedImageCache] clearDisk];//清理磁盘
[[SDImageCache sharedImageCache] clearMemory];//清理内存