Reputation: 7247
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?
Upvotes: 4
Views: 2707
Reputation: 4558
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"];
Upvotes: 8
Reputation: 11
You should change this:
[UIImage imageNamed:@"Default.png"];
to
[UIImage imageNamed:@"Default"];
Upvotes: 1
Reputation: 7247
I did it manually for all splash screens:
CGRect screenRect = [[UIScreen mainScreen] bounds];
float screenWidth = screenRect.size.width;
float screenHeight = screenRect.size.height;
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
splashView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight)];
if (screenHeight==568.0) {
splashView.image=[UIImage imageNamed:@"[email protected]"];//iPhone 5
}else{
splashView.image=[UIImage imageNamed:@"Default.png"]; //other iPhones
}
} else {
splashView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 20, screenWidth, screenHeight-20)];
splashView.image=[UIImage imageNamed:@"Default-Portrait.png"];// iPads
}
Upvotes: 3
Reputation: 38249
EDIT: check this and also this out.
U how use this line to provide splash screen whether u have retina or non-retina display
UIImageView *splashView =[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Default.png"]];
Application detects device display takes image accordingly.
If device has retina display then it takes [email protected] automatically.
Upvotes: 1