4

I have Default splash screens with the names: Default-568h@2x.png, Default-Portrait.png, Default.png, Default@2x.png 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?

Alexey
  • 7,127
  • 9
  • 57
  • 94

4 Answers4

8

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"];

ChrisH
  • 4,468
  • 2
  • 33
  • 42
  • if Iuse [UIImage imagedNamed:@"Default"]; iOS will detect ONLY non retina. To detect both versions Default.png should be used – Alexey Oct 04 '12 at 18:28
  • 1
    As long as your target is iOS 4 or higher, you should be able to use `@"Default"` without specifying the extension. Either way I've edited my example to handle extensions. – ChrisH Oct 04 '12 at 19:06
3

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:@"Default-568h@2x.png"];//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
}
Alexey
  • 7,127
  • 9
  • 57
  • 94
1

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 Default@2x.png automatically.

Community
  • 1
  • 1
Paresh Navadiya
  • 38,095
  • 11
  • 81
  • 132
1

You should change this:

[UIImage imageNamed:@"Default.png"];

to

[UIImage imageNamed:@"Default"];
Ben
  • 51,770
  • 36
  • 127
  • 149
iOSDev
  • 11
  • 1