Skip to content

Instantly share code, notes, and snippets.

@mrtj
Created February 15, 2013 11:56
Show Gist options
  • Save mrtj/4959969 to your computer and use it in GitHub Desktop.
Save mrtj/4959969 to your computer and use it in GitHub Desktop.
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;
@end
#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];
}
}
@end
#import <UIKit/UIKit.h>
@interface UIScreen (Retina4)
-(BOOL)isRetina4;
@end
#import "UIScreen+Retina4.h"
@implementation UIScreen (Retina4)
-(BOOL)isRetina4
{
return (self.scale == 2.f && self.bounds.size.height == 568.0f);
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment