Skip to content

Instantly share code, notes, and snippets.

Created February 15, 2013 11:56
What would you like to do?
Extends [UIImage imageNamed:] method to consider also -568h@2x suffices of images targeted for Retina 4" devices. #iOS #objective-c #retina #ui #utility
#import <UIKit/UIKit.h>
@interface UIImage (Retina4)
+ (UIImage *)imageNamedRetina4:(NSString *)imageName;
#import "UIImage+Retina4.h"
@implementation UIImage (Retina4)
+ (UIImage *)imageNamedRetina4:(NSString *)imageName {
if (![[UIScreen mainScreen] isRetina4]) return [UIImage imageNamed:imageName];
NSMutableString *imageNameMutable = [imageName mutableCopy];
NSRange retinaAtSymbol = [imageName rangeOfString:@"@"];
if (retinaAtSymbol.location != NSNotFound) {
NSLog(@"Warning: UIImage might misconfigure scale property if you include @2x in the image name!");
[imageNameMutable insertString:@"-568h" atIndex:retinaAtSymbol.location];
} else {
NSRange dot = [imageName rangeOfString:@"."];
if (dot.location != NSNotFound) {
[imageNameMutable insertString:@"-568h" atIndex:dot.location];
} else {
[imageNameMutable appendString:@"-568h"];
UIImage* image = [UIImage imageNamed:imageNameMutable];
if (image) {
return image;
} else {
return [UIImage imageNamed:imageName];
#import <UIKit/UIKit.h>
@interface UIScreen (Retina4)
#import "UIScreen+Retina4.h"
@implementation UIScreen (Retina4)
return (self.scale == 2.f && self.bounds.size.height == 568.0f);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment