sudo
sudo

Reputation: 5784

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.

Upvotes: 5

Views: 7817

Answers (5)

sudo
sudo

Reputation: 5784

Sorry I somehow forgot about this question! The answers were helpful in other ways, and I tried them all, but the solution I've found is that you need to mark any images you want appearing without scaling on a retina display by appending "@2x" before the extension in the file name. For example, "foo.png" and "[email protected]". Changing the content mode to something like UIViewContentModeCenter alone won't work.

The reason you need to do this is because iOS will scale up the non-retina images in a UIImageView to 4X the area to make them appear the same on retina and non-retina devices. If you want it to take advantage of all the pixels and not scale it up, you must indicate so in the filename with "@2x", and it will only use that image for retina devices.

Upvotes: 1

Shubhendu
Shubhendu

Reputation: 1091

You are using the code correctly

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

But you need to set its content mode to

[self.imageview setContentMode:UIViewContentModeScaleToFill];

Now this UIViewContentModeScaleToFill will fill the UIimage view with the image. Since you have set the uiimageview size same as of Image, therefore image will now be shown distorted.

Hope this will help you

Upvotes: 0

Zaraki
Zaraki

Reputation: 3730

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.

Upvotes: 9

lakshmen
lakshmen

Reputation: 29064

[self.imageView setContentMode:UIViewContentModeScaleAspectFit];

This would not causes the image to scale.

Upvotes: 0

Simon M
Simon M

Reputation: 2941

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.

Upvotes: 2

Related Questions