red_coder
red_coder

Reputation: 11

image sizes for different iOS devices

I'm new to iOS programming. I want to ask these questions about image sizes for different screen sizes

  1. Do x, 2x and 3x image sizes suffice for all the iOS devices? I mean if I have an image named "background.png", will background.png, background2x.png and background3x.png be sufficient for all the iOS devices/screen sizes?
  2. If not, do different iPad models require some other image sizes(other than x, 2x and 3x)? ....... A link for explaining image sizes for different screen sizes/devices will be appreciated. Thanks

Upvotes: 1

Views: 127

Answers (2)

emotality
emotality

Reputation: 13035

If you made images for @1x, @2x, and @3x, name them like this:

Then you can just call the blow method like:

UIImage *myImage = [self deviceSizedImageWithName:@"myImage.png"];

#define kScreenHeight [UIScreen mainScreen].bounds.size.height
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenSize [UIScreen mainScreen].bounds.size
#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
#define IS_IPHONE_4 (IS_IPHONE && kScreenHeight == 480.0f)
#define IS_IPHONE_5 (IS_IPHONE && kScreenHeight == 568.0f)
#define IS_IPHONE_6 (IS_IPHONE && kScreenHeight == 667.0f)
#define IS_IPHONE_6P (IS_IPHONE && kScreenHeight == 736.0f)

- (UIImage *)deviceSizedImageWithName:(NSString *)imageNamed
{
    NSString *imgExtension = [imageNamed pathExtension];
    NSString *imgName = [imageNamed stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@".%@", imgExtension] withString:@""];

    BOOL removedExt = [imgExtension length];

    UIImage *image = [UIImage imageNamed:imageNamed];
    if (IS_IPHONE_5) {
        if (removedExt) image = [UIImage imageNamed:[NSString stringWithFormat:@"%@-568h.%@", imgName, imgExtension]];
        else image = [UIImage imageNamed:[NSString stringWithFormat:@"%@-568h", imageNamed]];

        if (!image) return [UIImage imageNamed:imageNamed];

    } else if (IS_IPHONE_6) {
        if (removedExt) image = [UIImage imageNamed:[NSString stringWithFormat:@"%@-667h.%@", imgName, imgExtension]];
        else image = [UIImage imageNamed:[NSString stringWithFormat:@"%@-667h", imageNamed]];

        if (!image) return [UIImage imageNamed:imageNamed];

    } else if (IS_IPHONE_6P) {
        if (removedExt) image = [UIImage imageNamed:[NSString stringWithFormat:@"%@-736.%@", imgName, imgExtension]];
        else image = [UIImage imageNamed:[NSString stringWithFormat:@"%@-736", imageNamed]];

        if (!image) return [UIImage imageNamed:imageNamed];

    }
    return image;
}

Upvotes: 0

Anupam Mishra
Anupam Mishra

Reputation: 3588

yes x, 2x and 3x image sizes is necessary to develop application for all iOS devices , but the size of images can be different for iPad & iPhone devices, depending on your application UI for iPad & iPhone .

For better understanding for background images please have a look Adaptivity and Layout

One of the best article related to your query - Adaptive Layout Tutorial in iOS 9: Getting Started

Upvotes: 1

Related Questions