iOS Retina display: images double size

爷,独闯天下 提交于 2019-12-05 04:35:40

问题


For testing not/retina display I've created an UIView with size 100x100.

I've create 2 images: - normal size (100x100) - retina size (200x200)

I have two situations: 1) Non-Retina display + Normal Size image in background 2) Retina display + Retina Size image in background

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.

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];
}

回答1:


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.

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.




回答2:


You don't need to code the IS_RETINA test, just add the @2x suffix to the name of the file containing the image you want to use for the retina display, and iOS will automagically use that in preference.

Bundle 2 image files, prettyNavBarBackground.png and prettyNavBarBackground@2x.png

#define IMG_NAVIGATION_BAR_BACKGROUND prettyNavBarBackground

Then you can just use this single call, and iOS will select the appropriate option

[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:IMG_NAVIGATION_BAR_BACKGROUND] forBarMetrics:UIBarMetricsDefault];



回答3:


U don't have to write any sought of code to distinguish between retina and normal hardware. U just need to add a "@2x" image . This will work fine as perfect.

U don't have to call these images individually, but just write the filename.



来源:https://stackoverflow.com/questions/9368106/ios-retina-display-images-double-size

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!