问题描述
假设我在捆绑或资产目录中有三张图片:
Let's say I have three images in a bundle or asset catalog:
- 默认~iphone.png
- Default@2x~iphone.png
- [email protected]
在iOS 4及更高版本中,UIImage构造函数可以采用如下图像名称:
On iOS 4 and later, the UIImage constructor can take the image name as follows:
[UIImage imageNamed:@"Default"];
当我使用3.5英寸视网膜显示器(iphone)时,它会自动选择图像(2)。如果在非视网膜显示器上,则选择(1)。这很棒。
When I am on a 3.5 inch retina display (iphone) it automatically picks image (2). If on a non-retina display it picks (1). This is great.
我将图像命名为4英寸视网膜(iPhone 5)启动图像。有没有办法命名图像(3),所以当我在4英寸视网膜显示器上运行时,它会返回相同的UIImage构造函数?
I named image 3 as specified for the 4 inch retina (iPhone 5) launch image. Is there a way to name image (3), so that when I am running on a 4 inch retina display, it is returned with the same UIImage constructor?
也许这个尚未实现,或者我对方便性的期望过高......我只是试图避免代码中的任何条件逻辑根据屏幕尺寸选择图像。
Perhaps this is not implemented yet, or I expect too much from the convenience... I am just trying to avoid any conditional logic in my code to pick the image based on the screen dimensions.
推荐答案
我也有同样的问题,事实证明iPhone 5 / iPod Touch第五代没有这种行为。
I also had the same issue and it turned out that there is no such behavior for the iPhone 5/iPod Touch 5th Generation.
您必须手动确定您的应用程序是否在这样的设备上运行并相应地更改文件名。
You have to manually determine if your App is running on such a device and change the filename accordingly.
我已经使用此方法检查我的应用程序正在iPhone 5 / iPod Touch第五代上运行:
I've used this method to check if my App is running on an iPhone 5/iPod Touch 5th Gen.:
#define IS_PHONEPOD5() ([UIScreen mainScreen].bounds.size.height == 568.0f && [UIScreen mainScreen].scale == 2.f && UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
然后你可以像这样调整图像名称:
Then you can adjust the image name like this:
if(IS_PHONEPOD5()) {
myImageView.image = [UIImage imageNamed:@"MyImage-568h.png"];
} else {
myImageView.image = [UIImage imageNamed:@"MyImage.png"];
}
更新
我还在github上找到了一个UIImage类别(),它实现了你正在寻找的东西。它没有非现有文件的后备,但您可以自己轻松实现它。
Update
I also found a UIImage category on github (Link) that implements what you're looking for. It does not have a fallback for non existing files, but you could implement it easily by yourself.
这篇关于UIImage imageNamed:不会自动选择视网膜@ 2x图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!