Xcode 4.5背景图片iPhone 4,4s,5

时间:2012-10-25 14:03:15

标签: ios iphone ios6 screen iphone-5

我在viewController.m中编写了后台代码:

self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"image.png"]];

我有不同图片的正确名称:

image.png for non-retina display (320x480)

image@2x.png for retina display (640x960)

image-568h@2x.png for iPhone 5 (640x1136)

但是当我在模拟器中运行它时,它不需要图像-568h@2x.png用于iPhone 5屏幕它只需要图像@ 2x用于4s屏幕并缩放它以适应屏幕...我不知道如果有任何编码使用image-568h @ 2x for iPhone 5屏幕?

我正在使用Xcode 4.5

3 个答案:

答案 0 :(得分:13)

iPhone 5是视网膜,就像iPhone 4和4S一样,@ 2x图像将自动用于所有这些设备。它只是被称为" -568h @ 2x"的启动图像。对于iPhone 5.你需要编写一些代码来使用不同的图像,这样的东西会起作用:

NSString *filename = @"image.png";
CGRect screenRect = [[UIScreen mainScreen] bounds];
if (screenRect.size.height == 568.0f)
    filename = [filename stringByReplacingOccurrencesOfString:@".png" withString:@"-568h.png"];

imageView.image = [UIImage imageNamed:filename];

答案 1 :(得分:3)

如果您尝试使用[UIImage imageNamed:@"image.png"]并期望从iPhone 5的捆绑包中自动选择image-568h@2x.png,则无效。 自动拾取仅适用于iPhone 4和4S。

在iPhone 5中,只会自动选择名为Default-568h@2x.png的默认图像。

对于普通图像,如果iPhone 5有单独的图像,请尝试使用此代码

CGRect screenBounds = [[UIScreen mainScreen] bounds];
if (screenBounds.size.height == 568) {
    // code for 4-inch screen
} else {
    // code for 3.5-inch screen
}

答案 2 :(得分:2)

我认为假设您可以将-568h@2x技巧应用于所有图像文件是不正确的。我认为只有适用于Default-568h@2x.png。这是iOS在4英寸显示设备上的应用程序启动时查找的文件,以及在SDK中启用4“显示支持的”标志“。例如,一旦您包含此特定文件,您的表视图将填满屏幕。

我没有读过任何建议你可以简单地提供-568h@2x文件名组件的任何图像并让它自动使用的内容。你必须根据屏幕尺寸自己做,例如[UIScreen mainScreen].bounds.size.height