1.UIImage 的类目
//自己的 非系统的
+(UIImage *)imageWithNamed:(NSString *)name;
//引入runtime 框架库
#import <objc/runtime.h>
+(void)load{ //先执行编译,在执行main函数
NSLog(@"加载");
//在该文件加载到内存的时候
//exchange imageNamed: & imageWithName:
//获取方法名
Method system = class_getClassMethod([UIImage class], @selector(imageNamed:));//类方法
Method myfangfa = class_getClassMethod([UIImage class], @selector(imageWithNamed:));
//交换方法名
method_exchangeImplementations(system, myfangfa);
}
//方法的实现
+(UIImage *)imageWithNamed:(NSString *)name{
NSString *newName =[name stringByAppendingString:@"_ios9.1"];
UIImage *image = nil;
if ([UIDevice currentDevice].systemVersion.floatValue >= 9) {
image = [UIImage imageWithNamed:newName];
if (image) {
return image;
}
}
image = [UIImage imageWithNamed:name];
return image;
}
2."viewController"
UIImageView *imageView = [[UIImageView alloc]initWithFrame:self.view.bounds];
imageView.image = [UIImage imageNamed:@"1"];
[self.view addSubview:imageView];
NSLog(@"%.2f",[UIDevice currentDevice].systemVersion.floatValue);//系统版本号