In iOS runtime there is a function for exchanging method names, called Swizzle, and the following example swaps Imagewithname: With Imagenamed: Two methods to invoke the System method imagenamed: The actual call is Imagewithname:, all the picture names are stitched _os7, when the old project needs to change a set of pictures can be avoided one by one.
@implementation UIImage (Extension)/** * as long as the classification is loaded into memory, it is called 1 times */+ (void) load{ Method othermehtod = Class_ Getclassmethod (Self, @selector (imagewithname:)); Method Originmehtod = Class_getclassmethod (self, @selector (imagenamed:)); Exchange 2 methods of implementation Method_exchangeimplementations (Othermehtod, originmehtod);} + (UIImage *) Imagewithname: (NSString *) name{ BOOL iOS7 = [[Uidevice currentdevice].systemversion Floatvalue] >= 7.0; UIImage *image = nil; if (iOS7) { nsstring *newname = [name stringbyappendingstring:@ "_os7"]; Image = [UIImage imagewithname:newname];//here is actually called when the system method imagenamed: } if (image = = nil) { image = [UIImage Imagewithname:name]; } return image; @end
Swizzle Interchange Method Name