-
-
Save craftzdog/3784303 to your computer and use it in GitHub Desktop.
Swizzled UIImage imageNamed for iPhone 5
This file contains 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
// | |
// UIImage+Retina4.h | |
// StunOMatic | |
// | |
// Created by Benjamin Stahlhood on 9/12/12. | |
// Updated by noradaiko on 9/26/12. | |
// Copyright (c) 2012 DS Media Labs. All rights reserved. | |
// Copyright (c) 2012 noradaiko. All rights reserved. | |
// | |
#import <UIKit/UIKit.h> | |
@interface UIImage (Retina4) | |
@end |
This file contains 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
// | |
// UIImage+Retina4.m | |
// StunOMatic | |
// | |
// Created by Benjamin Stahlhood on 9/12/12. | |
// Updated by noradaiko on 9/26/12. | |
// Copyright (c) 2012 DS Media Labs. All rights reserved. | |
// Copyright (c) 2012 noradaiko. All rights reserved. | |
// | |
#import "UIImage+Retina4.h" | |
#import <objc/objc-runtime.h> | |
static Method origImageNamedMethod = nil; | |
@implementation UIImage (Retina4) | |
+ (void)initialize | |
{ | |
if(!origImageNamedMethod) | |
{ | |
origImageNamedMethod = class_getClassMethod(self, @selector(imageNamed:)); | |
/* | |
Method oldMethod = class_getClassMethod(self, @selector(imageNamed:)); | |
Method newMethod = class_getClassMethod(self, @selector(retina4ImageNamed:)); | |
NSLog(@"oldMethod name is %@, imp is %x", NSStringFromSelector(method_getName(oldMethod)), (NSInteger)method_getImplementation(oldMethod)); | |
NSLog(@"newMethod name is %@, imp is %x", NSStringFromSelector(method_getName(newMethod)), (NSInteger)method_getImplementation(newMethod)); | |
*/ | |
method_exchangeImplementations(origImageNamedMethod, | |
class_getClassMethod(self, @selector(retina4ImageNamed:))); | |
/* | |
NSLog(@"oldMethod name is %@, imp is %x", NSStringFromSelector(method_getName(oldMethod)), (NSInteger)method_getImplementation(oldMethod)); | |
NSLog(@"newMethod name is %@, imp is %x", NSStringFromSelector(method_getName(newMethod)), (NSInteger)method_getImplementation(newMethod)); | |
*/ | |
} | |
} | |
+ (UIImage *)retina4ImageNamed:(NSString *)imageName | |
{ | |
NSLog(@"Loading image named => %@", imageName); | |
NSMutableString *imageNameMutable = [imageName mutableCopy]; | |
NSRange retinaAtSymbol = [imageName rangeOfString:@"@"]; | |
if (retinaAtSymbol.location != NSNotFound) { | |
[imageNameMutable insertString:@"-568h" atIndex:retinaAtSymbol.location]; | |
} else { | |
CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height; | |
if ([UIScreen mainScreen].scale == 2.f && screenHeight == 568.0f) { | |
NSRange dot = [imageName rangeOfString:@"."]; | |
if (dot.location != NSNotFound) { | |
[imageNameMutable insertString:@"-568h" atIndex:dot.location]; | |
} else { | |
[imageNameMutable appendString:@"-568h"]; | |
} | |
} | |
} | |
UIImage* ret = [UIImage retina4ImageNamed:imageNameMutable]; | |
if (ret) | |
return ret; | |
else | |
return [UIImage retina4ImageNamed:imageName]; | |
} | |
@end |
This file contains 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
// | |
// UIImage+Retina4.h | |
// StunOMatic | |
// | |
// Created by Benjamin Stahlhood on 9/12/12. | |
// Updated by noradaiko on 9/26/12. | |
// Copyright (c) 2012 DS Media Labs. All rights reserved. | |
// Copyright (c) 2012 noradaiko. All rights reserved. | |
// | |
#import <UIKit/UIKit.h> | |
@interface UIImage (Retina4) | |
@end |
This file contains 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
// | |
// UIImage+Retina4.m | |
// StunOMatic | |
// | |
// Created by Benjamin Stahlhood on 9/12/12. | |
// Updated by noradaiko on 9/26/12. | |
// Copyright (c) 2012 DS Media Labs. All rights reserved. | |
// Copyright (c) 2012 noradaiko. All rights reserved. | |
// | |
#import "UIImage+Retina4.h" | |
#import <objc/runtime.h> | |
#import <objc/message.h> | |
static Method origImageNamedMethod = nil; | |
@implementation UIImage (Retina4) | |
+ (void)initialize | |
{ | |
if(!origImageNamedMethod) | |
{ | |
origImageNamedMethod = class_getClassMethod(self, @selector(imageNamed:)); | |
/* | |
Method oldMethod = class_getClassMethod(self, @selector(imageNamed:)); | |
Method newMethod = class_getClassMethod(self, @selector(retina4ImageNamed:)); | |
NSLog(@"oldMethod name is %@, imp is %x", NSStringFromSelector(method_getName(oldMethod)), (NSInteger)method_getImplementation(oldMethod)); | |
NSLog(@"newMethod name is %@, imp is %x", NSStringFromSelector(method_getName(newMethod)), (NSInteger)method_getImplementation(newMethod)); | |
*/ | |
method_exchangeImplementations(origImageNamedMethod, | |
class_getClassMethod(self, @selector(retina4ImageNamed:))); | |
/* | |
NSLog(@"oldMethod name is %@, imp is %x", NSStringFromSelector(method_getName(oldMethod)), (NSInteger)method_getImplementation(oldMethod)); | |
NSLog(@"newMethod name is %@, imp is %x", NSStringFromSelector(method_getName(newMethod)), (NSInteger)method_getImplementation(newMethod)); | |
*/ | |
} | |
} | |
+ (UIImage *)retina4ImageNamed:(NSString *)imageName | |
{ | |
NSLog(@"Loading image named => %@", imageName); | |
NSMutableString *imageNameMutable = [imageName mutableCopy]; | |
NSRange retinaAtSymbol = [imageName rangeOfString:@"@"]; | |
if (retinaAtSymbol.location != NSNotFound) { | |
[imageNameMutable insertString:@"-568h" atIndex:retinaAtSymbol.location]; | |
} else { | |
CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height; | |
if ([UIScreen mainScreen].scale == 2.f && screenHeight == 568.0f) { | |
NSRange dot = [imageName rangeOfString:@"."]; | |
if (dot.location != NSNotFound) { | |
[imageNameMutable insertString:@"-568h" atIndex:dot.location]; | |
} else { | |
[imageNameMutable appendString:@"-568h"]; | |
} | |
} | |
} | |
UIImage* ret = [UIImage retina4ImageNamed:imageNameMutable]; | |
if (ret) | |
return ret; | |
else | |
return [UIImage retina4ImageNamed:imageName]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment