Programming on iPhone: how to make UIImage in a real-time UIImageView show?

I have a UIImage that I want to show pixel for pixel on the iPhone 4 / 4S / 5 screen. It is important that it does not scale at all, but when I try to use setImage , this makes the image too large.

My UIImageView runs in UIStoryboard (since I'm really not familiar with this) and is set to redraw with all the rest of the default value. None of the other modes scale UIImage properly (EDIT: i.e. setting UIImageViews contentMode for other things will not 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 does not work. I tried halving each size and it is still off.

UPDATE: I think it scales to display the retina after the fact, because on both the retina of the iPhone 4 that I use and the non-retina simulator mode, the images use the same percentage of the screen. Is there a way I can set up a UIImage or UIImageView or project to “retina ready”?

UPDATE 2: Reducing the size of the image by halving each measurement. The iPhone 4 / 4S has two 2G / 3G / 3GS pixels. But this seems like a hack solution, and I'm not even sure if it uses the pixel density of the retina screen when I do this.

+4
source share
5 answers

If your UIImage is larger than your UIImageView, use:

 if image.size > imageView.size [self.imageView setContentMode:UIViewContentModeScaleAspectFit]; 

this will keep the image in proportion while it fits into the image,

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

  if image.size < imageView.size self.imageView.contentMode = UIViewContentModeCenter; 

it will contain the image in the center, in proportion and in full size.

+8
source

Try changing the contentMode property of your UIImageView to UIViewContentModeCenter :

 self.imageView.contentMode = UIViewContentModeCenter; 

This should lead to the fact that the image will not be scaled at all and will be displayed in the center in the UIImageView.

+1
source

Sorry, I somehow forgot about this issue! The answers were useful in other ways, and I tried all of them, but the solution I found is that you need to mark any images that you want to display without scaling on the retina screen by adding "@ 2x" before the extension in the file name. For example, "foo.png" and " foo@2x.png ". Changing the content mode only on the UIViewContentModeCenter will not work.

The reason you need to do this is because iOS will scale non-retina images in the UIImageView to 4X so that they look the same on both mesh and non-mesh devices. If you want it to use all the pixels and not scale it, you must specify this in the file name with "@ 2x" and it will use this image only for retina devices.

+1
source
 [self.imageView setContentMode:UIViewContentModeScaleAspectFit]; 

This will not scale the image.

0
source

You are using 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 the content mode

 [self.imageview setContentMode:UIViewContentModeScaleToFill]; 

Now this UIViewContentModeScaleToFill will populate the UIimage view with an image. Since you set the uiimageview to the same size as the image, the image will now be distorted.

Hope this helps you

0
source

Source: https://habr.com/ru/post/1483062/


All Articles