使用runtime无侵入检查sdImageCache

.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;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容