交换方法

#import "UIImage+AddImageFunc.h"
#import 

@implementation UIImage (AddImageFunc)

+ (void)load {

    Method methodOfOrigin = class_getClassMethod(self, @selector(imageNamed:));
    Method methodOfCurrent= class_getClassMethod(self, @selector(custom_imageNamed:));
    //交换两个方法
    method_exchangeImplementations(methodOfOrigin, methodOfCurrent);

}


/*
 * 外界主动调用 imageNamed: 的时候,相当于调用了custom_imageNamed:这个方法
 *
 **/
+ (UIImage *)custom_imageNamed:(NSString *)name {

    if (name.length == 0) {

        name = @"1";
    }

    /*
     *  1.当 主动调用 [self custom_imageNamed:name]
        2.因为已经交换了 所以相当于调用[self imageNamed:name];
     **/
    UIImage * image = [self custom_imageNamed:name];
    return image;

    return nil;
}

@end

你可能感兴趣的:(runtime)