简体   繁体   English

iPhone 5和iPhone 4的图像命名

[英]Image Naming for iPhone 5 vs iPhone 4

I have retina images for iPhone 5 and iPhone 4. The only difference is the height of the image, the resolutions are the same. 我有适用于iPhone 5和iPhone 4的视网膜图像。唯一的区别是图像的高度,分辨率相同。

When I specify the iPhone 4 image, I do myImage@2x.png and for iPhone 5 I do myImageh@2x.png 当我指定iPhone 4图像时,我执行myImage@2x.png,对于iPhone 5,我执行myImageh@2x.png

Then in code 然后在代码中

if (iPhone_5)
       ... imageNamed:@"myImageh@2x.png"];
else
       ... imageNamed:@"myImage@2x.png"];

The Problem: This works great on the iOS simulator, however on my actual iPhone 5, the image is enlarged to twice the size. 问题:这在iOS模拟器上效果很好,但是在我的实际iPhone 5上,图像放大了两倍。 When I do if (iphone_5) ... imageNamed:@"myImage.png"] it works fine on real iPhone 5 but simulator complains that it can't find the image file. 当我执行if (iphone_5) ... imageNamed:@"myImage.png"]它在真实的iPhone 5上可以正常工作,但模拟器抱怨找不到图像文件。

What's the right way to do this? 什么是正确的方法?

Edit: I forgot to say that my images are in a texture atlas because this is for a spritekit game. 编辑:我忘了说我的图像在纹理地图集中,因为这是一个spritekit游戏。 Does that change anything? 这会改变什么吗?

Dont add @2x in name. 不要在名称中添加@2x When you access your image using [UIImage imageNamed:@"image.png"] , the correct one will be chosen automatically. 当使用[UIImage imageNamed:@"image.png"]访问图像时,将自动选择正确的图像。

Just try 你试一试

if (iPhone_5)
       ... imageNamed:@"myImageh.png"];
else
       ... imageNamed:@"myImage.png"];

There is no naming convention for compiler to distinguish between iPhone 4 and iPhone 5 images(difference in height only). 编译器没有命名约定来区分iPhone 4和iPhone 5图像(仅高度不同)。 The @2x will only distinguish retina and non-retina images for all iOS device. @2x将仅区分所有iOS设备的视网膜图像和非视网膜图像。

Hope this helps. 希望这可以帮助。

Just code as [UIImage imageNamed:@"myImage.png"] , no need to add @2x . 只需将代码编码为[UIImage imageNamed:@"myImage.png"] ,无需添加@2x Depending on device and screen size, it will automatically take correct one. 根据设备和屏幕尺寸的不同,它将自动选择正确的一个。

Note: You must add both images in bundle( myImage.png & myImage@2x.png ). 注意:必须将两个图像都添加到bundle( myImage.png & myImage@2x.png )中。 Otherwise It won't work. 否则它将无法正常工作。

Use the new Asset Catalog functionality. 使用新的资产目录功能。 Create an image set, then change the device setting from Universal to Device Specific. 创建映像集,然后将设备设置从“通用”更改为“特定于设备”。 Check off iPhone and Retina 4 Inch. 检查iPhone和Retina 4英寸。 Then you can add three versions (@1x/@2x and R4) of the image. 然后,您可以添加图像的三个版本(@ 1x / @ 2x和R4)。 Then you can just use imageNamed:@"Image" and not have to worry about any checks for screen size etc. The system will do it all for you. 然后,您可以只使用imageNamed:@“ Image”,而不必担心屏幕大小等检查。系统将为您完成所有操作。

特定于设备

3个版本

The real problem was that the texture atlas wasn't being loaded properly. 真正的问题是纹理图集未正确加载。 See this question for the solution Sprite Kit Textures Acting Funny 看到这个问题的解决方案Sprite Kit Textures Acting Funny

Don't ever use file extension or "@2x" in imageNamed: 永远不要在imageNamed:使用文件扩展名或“ @ 2x” imageNamed:
Use the name without extension, like @"image". 使用不带扩展名的名称,例如@“ image”。
This way it will load the correct image based on device. 这样,它将基于设备加载正确的图像。

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

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