Retina icons (@2x) aren't being used when images are specified in code

夙愿已清 提交于 2019-11-27 21:18:15

问题


I place a play.png image onto my view. When the view initially loads, the iPhone 4 grabs the corresponding play@2x.png file and it looks great. However, when I tap the play button my code swaps it out for the pause.png file. Then, when I tap the pause.png to bring back the play.png it uses the original play.png file (not the @2x version like I thought it would automatically reference).

This is the code I tried to use:

[button setImage:[UIImage imageNamed:@"play.png"] forState:UIControlStateNormal];

So, if I swap files after the initial view load, do I have to manually specify the @2x version inside an IF statement? If so, is the UIScreen.scale the best attribute to use for this?

I'm currently using code like this:

if ([UIScreen mainScreen].scale > 1.0) 
{ 
    [button setImage:[UIImage imageNamed:@"play@2x.png"] forState:UIControlStateNormal]; 
} 
else 
{ 
    [button setImage:[UIImage imageNamed:@"play.png"] forState:UIControlStateNormal]; 
}

It's working fine but having the IF statement in there is annoying and seems a little fragile.

Thanks in advance to all you smarties out there.


回答1:


I can confirm that this is a problem with the 4.0 device. The problem is not that it does not load the @2x image, it does indeed, but still displays it at 72 DPI (causing it to be blurry).

This bug is fortunately fixed in 4.1 (tested in the emulator).




回答2:


The conditional statement is unnecessary. The following line is sufficient:

[button setImage:[UIImage imageNamed:@"play.png"] forState:UIControlStateNormal];

In iOS 4.0, the imageNamed: method automatically looks for the "@2x" filename suffix if the device is an iPhone 4 and has the retina display. In previous versions of iPhone OS, the imageNamed: method only looks for what you write (i.e., the lower-resolution image). This works because the iPhone 4 can't have a lower OS version then 4.0, so your retina display users will always have the higher resolution artwork.




回答3:


You can use just:

[UIImage imageNamed:@"play"]

Without the extension. This will load the @2x version if available and if the device has a x2 scale.

This will work for iOS4 or grater. However if you want to run your application in previous versions you could do the following:

UIImage* image = [UIImage imageNamed:@"play"]; // for iOS 4 or greater
if(!image)
    image = [UIImage imageNamed:@"play.png"]; // for previous iOS versions

The benefit is that this will work if at any moment you have @3x or any other version if Apple creates new devices or displays.

You can create an utility method to avoid doing this everywhere you need to load an image.

See: Supporting High-Resolution Screens, section "Loading Images into Your Application"




回答4:


Two silly mistakes (both of which I've made before) that can cause this problem:

  1. Accidentally naming the small versions @2x instead of the large ones
  2. Having the large versions be slightly missized (by one pixel)



回答5:


I ran into the same problem, then realized that my Windows Photoshop-exported .png files were .PNG files. Apparently the capitalization does matter.

Also see Hi-Res @2x image not being picked up for tab bar item




回答6:


Someone on another thread mentioned that they managed to solve a similarly vexing problem by deleting and re-adding their hi-res images to the project.




回答7:


I had similar issue because of filename - button_slice9.png and button_slice9@2x.png didn't work.

But button_slice.png and button_slice@2x.png works as expected in imageNamed:.




回答8:


I just had a similar problem that took a while to figure out. It turns out that my @2x images somehow weren't added to my app target, so they weren't being packaged.




回答9:


I had a similar problem when I replaced a low res icon-close.png with high res icon-close@2x.png. iPad builds seemed to ignore the "@2x" and loaded the image at double size with scale = 1.0. Removing the file and adding back did not help. Renaming it to "icon-leave@2x.png" did. Some bad info cached somewhere about icon-close.png



来源:https://stackoverflow.com/questions/3442239/retina-icons-2x-arent-being-used-when-images-are-specified-in-code

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