I have Default splash screens with the names: [email protected], Default-Portrait.png, Default.png, [email protected] and so on for all types of devices.
I know that the system automatically selects the appropriate splash screen for the specific device and displays it.
The questions: is it possible to know which image the system selected? How to load the appropriate image selected by the system to the UIimageView.
I tried this:
UIImageView *splashView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight)];
splashView.image=[UIImage imageNamed:@"Default.png"];
But it loads only the image with name Default.png for all types of devices(iPhone 4, 5, iPad).
Do i need to manage it manually? I mean to load the appropriate image after identifying the device type?
I found this question after running into the same problem. Seems that if you use [UIImage imagedNamed:@"Default"];
iOS will detect retina versus non-retina and apply the @2x
but it won't detect an iPhone 5 and apply the -568h
The solution I came up with was to write a category on UIImage that checks the main window's height and returns the appropriate image if it exists:
@interface UIImage (Compatible)
+ (UIImage *)compatibleImageNamed:(NSString *)name;
@end
@implementation UIImage (Compatible)
+ (UIImage *)compatibleImageNamed:(NSString *)name {
if ([[UIScreen mainScreen] bounds].size.height==568.0){
NSString *extension = [name pathExtension];
NSString *iPhone5Name = [[name stringByDeletingPathExtension] stringByAppendingString:@"-568h"];
if (extension.length!=0)
iPhone5Name = [iPhone5Name stringByAppendingPathExtension:extension];
UIImage *image = [UIImage imageNamed:iPhone5Name];
if (image)
return image;
}
return [UIImage imageNamed:name];
}
@end
Then wherever I know I want to load an image that also has an iPhone 5 version I use:
[UIImage compatibleImageNamed:@"MyImage"];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With