问题描述
我想为每个不同的设备(即iPhone 4S,iPhone 5,iPhone6,iPhone 6Plus等)显示不同的背景。我不是在讨论启动图像,而是在使用应用程序时显示应用程序的背景。
I would like to display a different background for each different device (i.e. iPhone 4S, iPhone 5, iPhone6, iPhone 6Plus etc.). I am not talking about launch images but app's backgrounds that will be displayed while using the app.
我在ViewController中添加了以下代码:
I have added the following code in my 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目录中。
And I am ready to add the assets into the Images.xcassets catalog.
这是我在创建时看到的内容一个新的图像集
This is what I see when I create a new "Image set"
因此,我试图将资产与每个不同的设备进行匹配。
Therefore, I am trying to match the assets with each different device.
感谢这个问题: 我现在知道这些设备将访问以下图片:
Thanks to this question: universal image support I now know that these devices will access the following images:
iPhone 4 / 4S / 6 - > 2x(图像尺寸: 640x960px)
iPhone 4/4S/6 -> 2x (image size: 640x960px)
iPhone 5 / 5c / 5s / iPod Touch - > Retina 4 2x(图像尺寸:640x1336)
iPhone 5/5c/5s/iPod Touch -> Retina 4 2x (image size: 640x1336)
iPhone 6 Plus - > 3x(图像尺寸:1242 x 2208)
iPhone 6 Plus -> 3x (image size: 1242 x 2208)
我的问题是,如果显然两个设备的尺寸不合适,iPhone 4/4和6如何访问相同的图像?
My question is, how can iPhones 4/4s and 6 access the same image if, clearly, it's not in the right size for both devices?
谢谢
推荐答案
尝试检查这个答案:
您也可以使用此代码配置不同的图像:
You can use this code to configure a different image as well:
NSNumber *screenWidth = @([UIScreen mainScreen].bounds.size.width);
NSString *imageName = [NSString stringWithFormat:@"name-%@w", screenWidth];
UIImage *image = [UIImage imageNamed:imageName];
这篇关于如何为不同的iPhone / iPad使用xcassets /通用背景图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!