如何为不同的iPhone / iPad使用xcassets /通用背景图像?

时间:2015-03-17 13:40:16

标签: ios xcode

我想为每个不同的设备(即iPhone 4S,iPhone 5,iPhone6,iPhone 6Plus等)显示不同的背景。我不是在讨论启动图像,而是在使用应用程序时显示应用程序的背景。

我在ViewController中添加了以下代码:

var bgImage     = UIImage(named: "main_bg");
var imageView   = UIImageView(frame: self.view.bounds);
imageView.image = bgImage
self.view.addSubview(imageView)
self.view.sendSubviewToBack(imageView)

我准备将资源添加到Images.xcassets目录中。

这是我在创建新的"图像集"

时看到的

enter image description here

因此,我试图将资产与每个不同的设备进行匹配。

感谢这个问题:universal image support我现在知道这些设备将访问以下图片:

  

iPhone 3 - > 1x(图像尺寸:320x480px)

     

iPhone 4 / 4S / 6 - > 2x(图像尺寸:640x960px)

     

iPhone 5 / 5c / 5s / iPod Touch - > Retina 4 2x(图像大小:640x1336)

     

iPhone 6 Plus - > 3x(图片大小:1242 x 2208)

我的问题是,如果两架设备的尺寸不合适,那么iPhone 4/4和6如何才能访问相同的图片?

谢谢

1 个答案:

答案 0 :(得分:2)

尝试检查这个答案: How to handle image scale on all the available iPhone resolutions?

您也可以使用此代码配置其他图片:

NSNumber *screenWidth = @([UIScreen mainScreen].bounds.size.width);
NSString *imageName = [NSString stringWithFormat:@"name-%@w", screenWidth];
UIImage *image = [UIImage imageNamed:imageName];