Extends [UIImage imageNamed:] method to consider also -568h@2x suffices of images targeted for Retina 4" devices. #iOS #objective-c #retina #ui #utility
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
#import <UIKit/UIKit.h> | |
@interface UIImage (Retina4) | |
+ (UIImage *)imageNamedRetina4:(NSString *)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
#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 |
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
#import <UIKit/UIKit.h> | |
@interface UIScreen (Retina4) | |
-(BOOL)isRetina4; | |
@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
#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