iOS Retina显示屏:图像双倍大小 [英] iOS Retina display: images double size
问题描述
为了测试不是/ retina显示我创建了一个大小为100x100的UIView。
For testing not/retina display I've created an UIView with size 100x100.
我创建了2张图片:
- 正常尺寸(100x100 )
- 视网膜尺寸(200x200)
I've create 2 images: - normal size (100x100) - retina size (200x200)
我有两种情况:
1)非Retina显示+背景中的正常尺寸图像
2)视网膜显示+背景中的Retina尺寸图像
I have two situations: 1) Non-Retina display + Normal Size image in background 2) Retina display + Retina Size image in background
第一种情况是可以的。
在第二种情况下,图像是双倍尺寸,在我的UIView中,我只能看到总图像的1/4。
The 1st scenario is ok. In the 2nd scenario the image is double size and in my UIView I can see only 1/4 of the total image.
当我尝试时也是如此将背景图像分配给我的UIViewController导航栏如下:
The same happens when I try to assign a background image to my UIViewController navigation bar as following:
if (IS_RETINA()) {
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:IMG_NAVIGATION_BAR_BACKGROUND_RETINA] forBarMetrics:UIBarMetricsDefault];
}
else {
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:IMG_NAVIGATION_BAR_BACKGROUND] forBarMetrics:UIBarMetricsDefault];
}
推荐答案
有一种更简单的方法做这个。为图像的视网膜版本提供与非视网膜版本相同的名称,最后除了@ 2x。例如,如果您的常规图像名为 foo.png
,则视网膜版本应命名为 foo@2x.png
。
There's a much easier way to do this. Give the retina version of the image the same name as the non-retina version, except with a "@2x" at the end. For example, if your regular image was named foo.png
, then the retina version should be named foo@2x.png
.
然后,只需要参考文件名的常规版本(例如, foo.png
)倍。当您的应用程序在非视网膜硬件上运行时,将使用常规图像,但无论何时使用视网膜硬件,都将自动使用更高分辨率的图像。它比为你使用的每个图像编写一个if语句更容易,而且它实际上也可以工作。
Then, just refer to the regular version of the filename (e.g., foo.png
) at all times. When your app is running on non-retina hardware the regular image will be used, but whenever you're on retina hardware the higher-resolution image will be used automatically. It's easier than having to write an if statement for every image you use, plus it'll actually work.
这篇关于iOS Retina显示屏:图像双倍大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!