最新版的SDWebImage已带有获取缓存空间总大小的方法 -(vode) getSize ;
若要想获取SDImageCache指定缓存大小 可以添加一下方法;
- (float)getCachesSize{
float getSize = 0;
NSDirectoryEnumerator *fileEnumerator = [[NSFileManager defaultManager] enumeratorAtPath:self.diskCachePath];
for ( NSString* fileName in fileEnumerator){
NSString* cachePath = [self.diskCachePath stringByAppendingPathComponent:fileName];
nser
NSError *error = nil;
NSDictionary* attrs = [[NSFileManager defaultManager] attributesOfItemAtPath:cachePath error:&error];
unsigned long long length = [attrs fileSize];
cacheSize += length / 1024.0 / 1024.0;
}
return cacheSize;
}
调用以上的方法便可以获得缓存大小,
-------------------------------又是一个宁静的夜晚------------------------------------
下面方法是清除缓存的方法,在指定目录的情况下,清除指定目录的文件,方法是通用的。
SDImageCache 类中也是带有清除缓存的方法 ;- (void)clearMemory; - (void)clearDisk; 主要使用第二个。
+ (void)cleanFileDirectoryPath{
NSString* pathFile = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES)[0];
NSString* cachePath = [pathFile stringByAppendingPathComponent:
@"Caches/com.hackemist.SDWebImageCache.default"];
if (cachePath) {
[[NSFileManager defaultManager] createDirectoryAtPath:cachePath withIntermediateDirectories:YES attributes:nil error:nil];
}
NSError* error = nil;
[[NSFileManager defaultManager] removeItemAtPath:cachePath error:&error];
[[NSFileManager defaultManager] createDirectoryAtPath:cachePath withIntermediateDirectories:YES attributes:nil error:&error];
}
当然调用这个方法也可以实现清除缓存的 ;(路径根据需要修改,这里的路径为SDImageCache的缓存路径)。