Last active
March 31, 2016 08:37
-
-
Save lamprosg/cec54b3311466cc2f633fee0fde8e92e to your computer and use it in GitHub Desktop.
(iOS) Swizzling Image Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #import <Foundation/Foundation.h> | |
| @interface NSObject (Swizzling) | |
| + (void)swizzleInstanceSelector:(SEL)originalSelector | |
| withNewSelector:(SEL)newSelector; | |
| @end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #import "NSObject+Swizzling.h" | |
| @implementation NSObject (Swizzling) | |
| + (void) swizzleInstanceSelector:(SEL)originalSelector | |
| withNewSelector:(SEL)newSelector | |
| { | |
| Method originalMethod = class_getClassMethod(self, originalSelector); | |
| Method newMethod = class_getClassMethod(self, newSelector); | |
| method_exchangeImplementations(originalMethod, newMethod); | |
| } | |
| @end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #import <UIKit/UIKit.h> | |
| @interface UIImage (UIImage_ImageProvider) | |
| +(UIImage *)realImageNamed:(NSString *)name; | |
| @end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #import "UIImage+UIImage_ImageProvider.h" | |
| #import "NSObject+Swizzling.h" | |
| @implementation UIImage (UIImage_ImageProvider) | |
| + (void)load { | |
| static dispatch_once_t onceToken; | |
| dispatch_once(&onceToken, ^{ | |
| [self swizzleInstanceSelector:@selector(imageNamed:) | |
| withNewSelector:@selector(realImageNamed:)]; | |
| }); | |
| } | |
| +(UIImage *)realImageNamed:(NSString *)name { | |
| NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:someImagePath]; | |
| UIImage *img = [UIImage imageWithContentsOfFile:dataPath]; | |
| if (img) { | |
| return img; | |
| } | |
| else { | |
| //Load the default | |
| return img= [self realImageNamed:name]; | |
| } | |
| } | |
| @end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment