1

Is there any simple way to achieve a 1:1 aspect ratio and scale to the original image inside an ImageView ?

I don't want only to keep the Image aspect ratio, I want it to be exactly the same size as the underlying image (1:1 scale also to original)... not smaller, not bigger... so it will be 1:1 on screen in therms of pixels or UIKit points or w/e with the original (scaling 1).

If you only set aspect ratio, the image might scale up or down, whilst keeping aspect ratio. I want to prevent this behaviour and to tell it not to scale.

How can I set the scale to be 1 as a constraint ? Is there any way in doing this, without any hardcoding on width / height constraints to match the size of the original image ?

Basically I want to tell the autolayout... here is the image, don't touch it, never scale it no matter what, let it be exactly as the original (scaling 1).

UPDATE:

The only way I could achieve this is by setting the hugging / compression priorities to higher than other items in the layout. Only setting aspect ratio constraint doesn't do it... I think there is no other way to ensure this behaviour without touching compression / hugging priorities.

Community
  • 1
  • 1
Alin Lipan
  • 219
  • 3
  • 14
  • Take a look at this answer: https://stackoverflow.com/a/42654375/2581843 – Sander Saelmans Oct 17 '18 at 14:55
  • I already looked there before posting this... I found that in my case alone, setting the image content hugging priority to high fixed the problem, and kept the image to the original, but without that it scaled it upwards. – Alin Lipan Oct 17 '18 at 14:57
  • You can add an aspect ratio constraint of 1:1 and change `UIImageView`'s `contentMode` to `Aspect Fill` also set `imageView.layer.maskToBounds = true`. Then give image a custom width like 100 then all the images you set to this `imageView` will have 300x300 size and if an image's original size is 200x200 then it will scale up to 300x300 even if the image's original aspect ratio is not `1:1` it'll still scale as `1:1`. – Ayazmon Oct 17 '18 at 14:59
  • Don't know if this should be the corrected answer though. I have constraints on positions, and I want to tell autolayout to keep it's intrinsic content size. By setting the hugging / compression priorities to high, the image doesn't get resized anymore. – Alin Lipan Oct 17 '18 at 14:59
  • @Ayazmon I want to avoid hardcoding pixel values as much as possible. I don't want to use numbers, only to tell autolayout to keep the intrinsic content size and not scale no matter what happens. – Alin Lipan Oct 17 '18 at 15:00
  • If you only constrain the position of the image view and not the size then it will always resize to be the size of whatever image is in it. – dan Oct 17 '18 at 15:11
  • Don't even think of ___not scaling___ the image. Because what happens when your provided image has a dimension of `2000 * 2000`? Is it going to be drawn on the iPhone's screen beyond it's screen size?? No!, not really. – nayem Oct 18 '18 at 06:50

3 Answers3

0

Set the contentMode to aspectFit.

See Apple official documentation :

The option to scale the content to fit the size of the view by maintaining the aspect ratio. Any remaining area of the view’s bounds is transparent.

Marwen Doukh
  • 1,946
  • 17
  • 26
0

Don't put any height or aspect ratio constraint to your UIImageView just put the width constraint or leading and trailing constraint whatever suits your need. And make contentMode AspectFill.

ImageView will scale automatically to match the aspect ratio of the image it holds. However make sure to have a height <= the maximum height you should have(In the images below I should put height <= the screen height) so that the image doesn't go beyond the desired bounds.

enter image description here enter image description here

Roohul
  • 1,009
  • 1
  • 15
  • 26
0

Try setting the ContentMode of your ImageView as Center.

Also remember to set clip to bounds.

imageView.clipsToBounds = true
imageView.contentMode = .center
vicegax
  • 4,709
  • 28
  • 37