Skip to content

Instantly share code, notes, and snippets.

@samstewart
Created August 30, 2013 19:09
Show Gist options
  • Save samstewart/6393270 to your computer and use it in GitHub Desktop.
Save samstewart/6393270 to your computer and use it in GitHub Desktop.
Loads iPad and iPhone images according to Apple's naming convention. Nice for retina displays
//
// UIImage+CompatibleImage.m
// Notescast 3.6
//
// Created by Sam Stewart on 8/25/13.
//
//
#import "UIImage+CompatibleImage.h"
@implementation UIImage (CompatibleImage)
+ (UIImage*)compatibleImage:(NSString*)basename {
// build up the string
NSString *ext = [basename pathExtension];
basename = [basename stringByDeletingPathExtension];
float scale = [[UIScreen mainScreen] scale];
BOOL isiPhone5 = (CGRectGetHeight([UIScreen mainScreen].bounds) * scale == 568.0);
if (isiPhone5) {
basename = [basename stringByAppendingString:@"-568h"];
} else {
if (UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation) && ! isiPhone5) {
basename = [basename stringByAppendingString:@"-Landscape"];
} else {
basename = [basename stringByAppendingString:@"-Portrait"];
}
}
if (scale == 2.0) {
basename = [basename stringByAppendingString:@"@2x"];
}
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
basename = [basename stringByAppendingString:@"~ipad"];
} else {
basename = [basename stringByAppendingString:@"~iphone"];
}
basename = [basename stringByAppendingPathExtension:ext];
NSLog(@"Compatible Image Name: %@", basename);
return [UIImage imageNamed:basename];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment