Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone programming: How do I make a UIImage in a UIImageView show at actual size?

I have a UIImage that I want to show pixel-for-pixel on an iPhone 4/4S/5 screen. It's important that it is not scaled at all, but when I try using setImage, it makes the image too large.

My UIImageView is made in a UIStoryboard (since I'm really new to this) and is set to the mode "redraw" with everything else default. None of the other modes are scaling the UIImage properly (EDIT: that is, setting the UIImageViews contentMode to other things won't work).

I looked around and found this:

[self.imageView setImage: image];
self.imageView.frame = CGRectMake(self.imageView.frame.origin.x, 
                                  self.imageView.frame.origin.y,
                                  image.size.width, 
                                  image.size.height);

which doesn't work. I tried halving each dimension, and it's still off.

UPDATE: I think that it is being scaled for the retina display after the fact because on both the retina iPhone 4 I am using and the non-retina simulator mode, the images use up the same percentage of the screen. Is there some way I can set the UIImage or UIImageView or project to be "retina-ready"?

UPDATE 2: Making the image smaller by halving each dimension appears to work. The iPhone 4/4S has double the pixels of the 2G/3G/3GS. But this seems like a hack solution, and I'm not even sure if it's taking advantage of the retina display pixel density when I do that.

like image 874
sudo Avatar asked May 28 '13 02:05

sudo


2 Answers

If the size of your UIImage is greater than the size of your UIImageView then use:

if image.size > imageView.size 

[self.imageView setContentMode:UIViewContentModeScaleAspectFit];

this will keep you image in proportion while it will fit inside the image view,

but, if the size of you UIImage is smaller than the size of your UIImageView then use:

 if image.size < imageView.size 

 self.imageView.contentMode = UIViewContentModeCenter;

it will keep you image at center, in proportion and at its full size.

like image 150
Zaraki Avatar answered Nov 01 '22 18:11

Zaraki


Try changing the contentMode property of your UIImageView to UIViewContentModeCenter:

self.imageView.contentMode = UIViewContentModeCenter;

That should cause the image to not scale at all, and show up centered within the UIImageView.

like image 41
Simon M Avatar answered Nov 01 '22 18:11

Simon M