问题:
上面的时候框架会缓存图片数据:
[UIImage imageNamed:]
我不希望缓存发生,所以我可以将其替换为
[UIImage imageOfContentOfFile:]
似乎解决了,但经过我的测试,在xib的实例化过程中,框架使用imageNamed:而不是imageOfContentOfFile。
也就是说,xib 加载的图像仍然被缓存。
所以我尝试覆盖 UIImage 的 imageNamed: 方法,使所有这些方法都不要缓存。
--试一试。类别:
@implementation UIImage (ImageNamedNoCache)
+ (UIImage *)imageNamed:(NSString *)name;
{
NSString* path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
return [UIImage imageWithContentsOfFile:path];
}
@end
--result:warning."Category 正在实现一个方法,该方法也将由其主类实现"
--尝试2。运行时替换方法的 imp
//get super method
Method method_imageNamed = class_getClassMethod([UIImage class], @selector(imageNamed:));
//get my method
Method method_myImageNamed = class_getClassMethod([self class], @selector(myImageNamed:));
//get my imp
IMP imp_myImageNamed = method_getImplementation(method_myImageNamed);
//set super method's imp to my imp
method_setImplementation(method_imageNamed, imp_myImageNamed);
--结果
这些也只在我调用 [UIImage imageNamed:]
时才起作用
但是 xib 的实例化不起作用。
帮帮我,谢谢
最佳答案
听起来像是 method swizzling 的工作.我发现它既危险又不必要,但如果您愿意, 是一个选择。使用风险自负。
关于objective-c - 如何覆盖框架类(运行时)中的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12613673/