Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load retina (high quality) images programmatically (on buttons)

Tags:

ios

I have a problem that I just haven't been able to figure out. I have an app done that will take a picture from the front and one from the back camera, then merge them together.

The name of the app is GroupCam

However I have an issue in where my UIButtons will not be loaded as retina images from the start. If i set them as @2x on the interface builder they do appear in high quality but once I reload them programmatically the low versions will appear instead.

This is due to the fact that, after a picture is taken and the screen changes the button images change, but when the user presses the back button the camera icons will look horrible (because the low quality versions are being loaded).

Please check the following pictures:

First Screen: Only the upper right button appears distorted because I am checking the value of the camera flash and "loading" the corresponding image programatically.

First Screen

Second Screen: The user has moved to another screen and came back to the first one, all buttons were reloaded and they now all look distorted.

Second Screen

Why is this happening?

Edit:

This is how I load the images.

[self.flashButton setImage:[UIImage imageNamed:@"flashOff"] 
                          forState:UIControlStateNormal];

I read somewhere on the internet that doing this will automatically load the most suitable version of the image.

I also tried this but the result is the same:

[self.takeButton setImage:[UIImage imageNamed:@"takePhoto.png"] forState:UIControlStateNormal];
like image 293
Pochi Avatar asked May 28 '12 10:05

Pochi


2 Answers

First off, its probably worth verifying that the low-res versions are actually being displayed. I had jumped to a similar assumption recently, swapped out the low-res version of the image with something different, and confirmed that the hi-res version was actually still being displayed, but was being distorted.

My actual problem, I had set

<uibutton_superview>.layer.shouldRasterize = YES;

If any of the buttons ancestors have this set, it could likely be your problem.

like image 152
Eoin Avatar answered Sep 22 '22 01:09

Eoin


If your app is taking a picture from the front camera and one from the back camera and merging them together, then the device requires a front facing camera. All iOS devices which have front facing cameras also have retina display's (so long as your not developing for the iPad 2, but that's a different story) so you shouldn't really have to have normal images at all, so all your images should be retina images. If there are no normal images, then this issue shouldn't happen at all. Make a backup copy, and try deleting all the normal images, and then try. Because your using the UIImage imageNamed:@"ImageName", like you said, should automatically select the image! Good Luck!

like image 39
sridvijay Avatar answered Sep 23 '22 01:09

sridvijay