不同iOS设备的图像大小

时间:2015-11-03 09:41:02

标签: ios

我是iOS编程的新手。我想问一下这些关于不同屏幕尺寸的图像尺寸的问题

  1. x,2x和3x图像尺寸是否适合所有iOS设备?我的意思是如果我有一个名为“background.png”的图像,那么对于所有iOS设备/屏幕尺寸,background.png,background2x.png和background3x.png是否足够?
  2. 如果没有,不同的iPad型号是否需要其他一些图像尺寸(x,2x和3x除外)? .......将会理解用于解释不同屏幕尺寸/设备的图像尺寸的链接。感谢

2 个答案:

答案 0 :(得分:2)

是x,2x和3x图像大小是开发所有iOS设备应用程序所必需的,但iPad和iPad的图像大小可能不同。 iPhone设备,取决于您的iPad和iPad的应用程序UI iPhone。

为了更好地了解背景图片,请查看Adaptivity and Layout

与您的查询相关的最佳文章之一 - Adaptive Layout Tutorial in iOS 9: Getting Started

答案 1 :(得分:0)

如果您为@ 1x,@ 2x和@ 3x制作了图像,请将它们命名为:

  • iPhone 3:myImage.png
  • iPhone 4,4S:myImage@2x.png
  • iPhone 5,5S:myImage-568h@2x.png
  • iPhone 6,6S:myImage-667h@2x.png
  • iPhone 6P,6PS:myImage-736h@3x.png

然后你可以像下面这样打电话:

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;
}