.h
#import "SDImageCache.h"
@interface SDImageCache (ZYCheckCleanCache)
- (float)checkTmpSize;
@end
.m
#import "SDImageCache+ZYCheckCleanCache.h"
#import <objc/runtime.h>
@implementation SDImageCache (ZYCheckCleanCache)
// http://www.wahenzan.com/a/mdev/ios/2015/0113/1484.html
- (float)checkTmpSize {
float totalSize = 0;
NSDirectoryEnumerator *fileEnumerator = [[NSFileManager defaultManager] enumeratorAtPath:[self getPrivateProperty:@"diskCachePath"]];
for (NSString *fileName in fileEnumerator) {
NSString *filePath = [[self getPrivateProperty:@"diskCachePath"] stringByAppendingPathComponent:fileName];
NSDictionary *attrs = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil];
unsigned long long length = [attrs fileSize];
totalSize += length / 1024.0 / 1024.0;
} // NSLog(@"tmp size is %.2f",totalSize); return totalSize;
return totalSize;
}
// 获取私有属性
// http://blog.csdn.net/qqmcy/article/details/50531812
- (id)getPrivateProperty:(NSString *)propertyName
{
Ivar iVar = class_getInstanceVariable([self class], [propertyName UTF8String]);
if (iVar == nil) {
iVar = class_getInstanceVariable([self class], [[NSString stringWithFormat:@"_%@",propertyName] UTF8String]);
}
id propertyVal = object_getIvar(self, iVar);
return propertyVal;
}
网友评论