Xcode 6如何为iPhone 5和6设备分别设置@2x图像?

时间:2020-12-27 22:51:23

I want to specify different background images for iPhone each device. 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. So no problem for Launch Images.

在LaunchImage中,可以为Retina HD 4.7设备指定图像。发射图像没问题。

Xcode 6如何为iPhone 5和6设备分别设置@2x图像?

In .xcassets file, I have option for 1x, 2x, Retina 4 2x and 3x. But there is no option for iPhone 6 (1334x750) device.

在.xcassets文件中,我可以选择1x、2x、Retina 42x和3x。但iPhone 6 (1334x750)没有选择。

Xcode 6如何为iPhone 5和6设备分别设置@2x图像?

So how to provide 1334x750 px image for iPhone 6 device? Looking for some solution using .xcassets file, not by programmatically loading images for each device.

那么如何为iPhone 6设备提供1334x750像素的图像呢?使用.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? for development

iPhone 6 Plus分辨率混乱:Xcode还是苹果网站?发展

xcode 6 asset catalog iphone 6

xcode 6资产目录

3 个解决方案

#1


1  

set your image name like this;

像这样设置你的图像名称;

image-320@2x//iPhone 5

图像- 320 @2x / / iPhone 5

image-375@2x//iPhone 6

图像- 375 @2x / / iPhone 6

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

#2


1  

Images.xcassets has option to provide separate image for iPhone 5(Retina 4 @2x), iPhone 6(@2x) and iPhone 6 plus(3x).

图像。xcassets可以为iPhone 5(Retina 4 @2x)、iPhone 6(@2x)和iPhone 6 plus(3x)提供单独的图像。

Xcode 6如何为iPhone 5和6设备分别设置@2x图像?

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

没有选择单独的图像与图像。xcassets在Xcode 7中,我们必须对所有视网膜设备使用相同的2x图像,对视网膜高清设备使用3x图像

#3


0  

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. It has several methods with boolean returns you can use to get various information about the device:

或者,您可以使用UIDevice类获取该信息。它有几种布尔返回的方法,你可以用它们来获取关于设备的各种信息:

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

OR, you can include <sys/sysctl.h> and;

或者,您可以包括 ;

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.

还有一些其他的编程方法,但是到目前为止,在我所知道的接口构建器中还没有明确的方法来实现这一点。请随时指出我的错误。

#1


1  

set your image name like this;

像这样设置你的图像名称;

image-320@2x//iPhone 5

图像- 320 @2x / / iPhone 5

image-375@2x//iPhone 6

图像- 375 @2x / / iPhone 6

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

#2


1  

Images.xcassets has option to provide separate image for iPhone 5(Retina 4 @2x), iPhone 6(@2x) and iPhone 6 plus(3x).

图像。xcassets可以为iPhone 5(Retina 4 @2x)、iPhone 6(@2x)和iPhone 6 plus(3x)提供单独的图像。

Xcode 6如何为iPhone 5和6设备分别设置@2x图像?

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

没有选择单独的图像与图像。xcassets在Xcode 7中,我们必须对所有视网膜设备使用相同的2x图像,对视网膜高清设备使用3x图像

#3


0  

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. It has several methods with boolean returns you can use to get various information about the device:

或者,您可以使用UIDevice类获取该信息。它有几种布尔返回的方法,你可以用它们来获取关于设备的各种信息:

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

OR, you can include <sys/sysctl.h> and;

或者,您可以包括 ;

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.

还有一些其他的编程方法,但是到目前为止,在我所知道的接口构建器中还没有明确的方法来实现这一点。请随时指出我的错误。