Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to load appropriate Default.png to UImageView?

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?

like image 227
Alexey Avatar asked Dec 12 '22 22:12

Alexey


1 Answers

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

like image 157
ChrisH Avatar answered Jan 10 '23 02:01

ChrisH