简体   繁体   English

Xcode 6如何为iPhone 5和6设备设置单独的@ 2x图像?

[英]Xcode 6 how to set separate @2x images for iPhone 5 and 6 devices?

I want to specify different background images for iPhone each device. 我想为iPhone每个设备指定不同的背景图像。 Like this 像这样

iPhone 6 Plus   1242x2208 pixels    bg@3x.png
iPhone 6        750x1334  pixels    bg@2x.png
iPhone 5        640x1136  pixels    bg@2x.png
iPhone 4        640x960   pixels    bg@2x.png
iPhone 3GS      320x480   pixels    bg.png

In LaunchImage there is option available to specify images for Retina HD 4.7 device. LaunchImage ,可以选择为Retina HD 4.7设备指定图像。 So no problem for Launch Images. 所以启动图像没问题。

在此输入图像描述

In .xcassets file, I have option for 1x , 2x , Retina 4 2x and 3x . .xcassets文件中,我有1x2xRetina 4 2x3x But there is no option for iPhone 6 (1334x750) device. 但是iPhone 6 (1334x750)设备没有选择。

在此输入图像描述

So how to provide 1334x750 px image for iPhone 6 device? 那么如何为iPhone 6设备提供1334x750 px图像呢? Looking for some solution using .xcassets file, not by programmatically loading images for each device. 使用.xcassets文件寻找一些解决方案,而不是通过以编程方式为每个设备加载图像。

I have already visited the related SO's questions, but none of them answers specifically for iPhone 6 device images. 我已经访问了相关SO的问题,但没有一个专门针对iPhone 6设备图像。

iPhone 6 Plus resolution confusion: Xcode or Apple's website? iPhone 6 Plus分辨率混乱:Xcode还是Apple的网站? for development 发展

xcode 6 asset catalog iphone 6 xcode 6资产目录iphone 6

set your image name like this; 像这样设置你的图像名称;

image-320@2x//iPhone 5 image-320 @ 2x // iPhone 5

image-375@2x//iPhone 6 image-375 @ 2x // iPhone 6

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

Images.xcassets has option to provide separate image for iPhone 5(Retina 4 @2x), iPhone 6(@2x) and iPhone 6 plus(3x). Images.xcassets可以选择为iPhone 5(Retina 4 @ 2x),iPhone 6(@ 2x)和iPhone 6 plus(3x)提供单独的图像。

在此输入图像描述

There is no option for iPhone 4 asset(probably apple is stopping iPhone 4 support) rather it takes @2x images (scale mode) for iPhone 4 also but you can do something like This post for iPhone 4 没有iPhone 4资产的选项(可能苹果正在停止支持iPhone 4),而iPhone 4也需要@ 2x图像(缩放模式),但你可以做类似iPhone 4的帖子

Update Xcode 7 更新Xcode 7

There is no option for separate image with Images.xcassets now in Xcode 7, We have to use same 2x images for all Retina devices and 3x for Retina HD device 现在Xcode 7中没有与Images.xcassets单独图像的选项,我们必须为所有Retina设备使用相同的2x图像,为Retina HD设备使用3x图像

If you want to explicitly do different stuff on different devices, you need to do it in code for now, like Valar Morghulis said . 如果你想在不同的设备上明确地做不同的东西,你现在需要在代码中进行,就像Valar Morghulis说的那样

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

OR, you can use UIDevice class to get that information. 或者,您可以使用UIDevice类来获取该信息。 It has several methods with boolean returns you can use to get various information about the device: 它有几个带布尔返回的方法,您可以使用它们来获取有关设备的各种信息:

[[UIDevice currentDevice] platform]
[[UIDevice currentDevice] hasRetinaDisplay]
[[UIDevice currentDevice] hasMultitasking]

OR, you can include <sys/sysctl.h> and; 或者,您可以包含<sys/sysctl.h>和;

NSString *platform = [self platform];

if ([platform isEqualToString:@"iPhone3,1"])    return @"iPhone 4";
if ([platform isEqualToString:@"iPhone3,3"])    return @"Verizon iPhone 4";
if ([platform isEqualToString:@"iPhone4,1"])    return @"iPhone 4S";
if ([platform isEqualToString:@"iPhone5,1"])    return @"iPhone 5 (GSM)";
if ([platform isEqualToString:@"iPhone5,2"])    return @"iPhone 5 (GSM+CDMA)";

so on and so forth. 等等等等。 You can learn about device strings at here . 您可以在此处了解设备字符串。

There are some other ways to do this programmatically, But as of now, there is no explicit way to do this in the interface builder that I know of. 还有其他一些以编程方式执行此操作的方法,但截至目前,在我所知的界面构建器中没有明确的方法可以执行此操作。 Feel free to point out my mistakes. 随意指出我的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM