美文网首页
使用runtime无侵入检查sdImageCache

使用runtime无侵入检查sdImageCache

作者: wustzhy | 来源:发表于2017-10-12 16:14 被阅读22次

.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;
}

相关文章

网友评论

      本文标题:使用runtime无侵入检查sdImageCache

      本文链接:https://www.haomeiwen.com/subject/ifloyxtx.html