您的位置:首页 > 移动开发 > IOS开发

利用RunTime实现iOS不同版本下使用不同的图片

2016-01-29 21:28 495 查看

1.UIImage 的类目

.h

//自己的
非系统的

+(UIImage *)imageWithNamed:(NSString *)name;

.m

//引入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);//系统版本号
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: