Atma
Atma

Reputation: 29767

ios use different images on iPhon4s vs iPhone 5

I have two images that I want to display. One that is sized for a iPhone 4s and one for a 5/5s. I have named the images display.png and [email protected] thinking that the lower resolution iPhone 4s would show display.png and the iPhone 5s would show [email protected]. However, both phones show [email protected] and the image is cutoff on the iPhone 4s.

How can make my app distinguish between the two different sized files?

Upvotes: 1

Views: 136

Answers (5)

Popeye
Popeye

Reputation: 12093

According to the Apple iOS Human Interface Guidelines and the Apple iOS App Programming Guide launch images that have -568h at the end will be used for iPhone5.

At runtime, the system looks for a launch image whose name contains the -568h modifier. If such an image is present, the system assumes that your app supports the iPhone 5 explicitly and runs it in fullscreen mode.

Images with the suffix -568h should be used for iPhone 5 screen size, images with @2x suffix will be for non-iPhone 5 screens but all other retina screens and finally images without a suffix are for non-retina screen devices such as iPhone 3GS. Check out the Apple Documentation above for a description of every image size and resolution and what the names should contain.

You can use the assets catalog to help you set these images up and the correct names, check out the Apple Documentation on Asset Catalog.

For none launch images that don't pick up the -568h here is a little macro to help you out.

#define ASSET_SCREEN_SIZE(normal, stretched) (([[UIScreen mainScreen] bounds].size.height <= 480.0) ? normal : stretched)

then all you need do is call

UIImage *imageToUse = [UIImage imageNamed:ASSET_SCREEN_SIZE(@"myImage", @"myImage-568h")];

Upvotes: 2

Matteo Gobbi
Matteo Gobbi

Reputation: 17697

iPhone 4s is retina and so get the image ending with @2x if exists.

So you should check if is an iPhone 5 and so load the properly image (that you can call with a particular end name).

To use a clean and well structured code, I would subclass the UIImage (or using category) creating a new method and then returning the appropriate image after checking if is an iPhone 5, calling [UIImage imageNamed:newName];

For example having images like:

name
name@2x
name-h568

Use this function:

#import "UIImage+ScreenSize.h"

#define IS_IPHONE_5 ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 ) < DBL_EPSILON )

@implementation UIImage (ScreenSize)


+ (UIImage *)imageScreenNamed:(NSString *)name {

    if (IS_IPHONE_5) {
        name = [name stringByAppendingString:@"-h568"]; //Or your favorite suffix
    }

    return [[self class] imageNamed:name];
}

@end

calling:

[UIImage imageScreenNamed:@"name"];

Upvotes: 1

Abhinav Singh
Abhinav Singh

Reputation: 8100

Have a look at Asset Catalog in Xcode 5+

enter image description here

2x is for 4s and R4 for iphone 5

enter image description here

Upvotes: 2

Brian Huang
Brian Huang

Reputation: 226

Because iphone4s is also using retina screen, so they will use [email protected] image instead of display.png. (assume you use "[email protected]" for iphone5, and use "display.png" for iphone 4s. p.s you should name them like "iphone5.png" and "iphone4.png" instead.)

1 So what you need to do is checking the size of height of this device, do sth like this:

#define IS_IPHONE_5 ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 ) < DBL_EPSILON )

then: use it as:

if (IS_IPHONE_5) { //use [email protected] } else { //use display.png }

2 Not sure about this one works:

You can try adding image named "[email protected]" for iphone5 version.

Upvotes: 0

mbm29414
mbm29414

Reputation: 11598

You just have to have two images. The "@2x" designation is for the difference between a retina and a non-retina screen. If you want different images for different devices, you need to have different images with different names.

Upvotes: 1

Related Questions