当一个APP用户量大的时候,就须要给不一样的用户作标签,用来彰显身份.好比QQ的会员,VIP等不一样的皮肤功能.git
imageNamed
方法替换成本身的方法MC_imageNamed
.在这方法里面进行身份/权限的判断.拼接不一样的图片名称.达到换肤的目的.#import "UIImage+Helper.h" #import <objc/runtime.h> @implementation UIImage (Helper) + (void)load { // 获取UIImage的imageNamed方法 Method imageNameddd = class_getClassMethod([UIImage class], @selector(imageNamed:)); // 获取MC_imageNamed方法 Method MC_imageNamed = class_getClassMethod([UIImage class], @selector(MC_imageNamed:)); // 方法替换 method_exchangeImplementations(imageNameddd, MC_imageNamed); } + (nullable UIImage *)MC_imageNamed:(NSString *)name { // 用户的权限 NSInteger roleId = 0; if (roleId == 0) { name = [NSString stringWithFormat:@"%@_normal",name]; } else { name = [NSString stringWithFormat:@"%@_vip",name]; } return [self MC_imageNamed:name]; } @end
imageView.image = [UIImage imageNamed:@"status"];
三.说明
1.须要导入<objc/runtime.h>
不导入会报错
Declaration of 'Method' must be imported from module 'ObjectiveC.runtime' before it is required
github
Objective-C语言是一门动态语言,它将不少静态语言在编译和连接时期作的事放到了运行时来处理。这种动态语言的优点在于:咱们写代码时可以更具灵活性,如咱们能够把消息转发给咱们想要的对象,或者随意交换一个方法的实现等。这种特性意味着objective-c不只须要一个编译器,还须要一个运行时系统来执行编译的代码。对于Objective-C来讲,这个运行时系统就像一个操做系统同样:它让全部的工做能够正常的运行。这个运行时系统即Objc Runtime。Objc Runtime实际上是一个Runtime库,它基本上是用C和汇编写的,这个库使得C语言有了面向对象的能力。runtime的强大之处在于它能在运行时建立类和对象。objective-c
2.换肤demo下载地址网络