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

5

5

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.

Vernation answered 28/5, 2013 at 2:31 Comment(0)
B
9

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.

Bushmaster answered 28/5, 2013 at 4:22 Comment(1)
And that because apple sucksGretta
A
2

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.

Arrowroot answered 28/5, 2013 at 2:39 Comment(1)
Thanks for the response. I tried that, but it still doesn't work. I think that the problem is that it scales everything for the retina display after the fact because I tried this on a retina iPhone 4 and a simulated non-retina iPhone, and the image always takes up the same percentage of width/height of the screen. Added to original question.Vernation
V
1

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.

Vernation answered 15/7, 2014 at 16:29 Comment(0)
A
0
[self.imageView setContentMode:UIViewContentModeScaleAspectFit];

This would not causes the image to scale.

Analogous answered 28/5, 2013 at 3:25 Comment(0)
C
0

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

Crystlecs answered 15/7, 2014 at 10:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.