Adjust UIImage with Aspect Ratio using Autolayout - ios

Is it possible to automatically resize uiimage based on autolayout with aspect ratio (1:1)?
Btw I'm using collectionview as my view.
Here's my layout in storyboard
and here's the result when running the app

You have to add as in picture to make image sacrifice width to fit to aspect ratio constraint

If you want to have the UIImageView to always be a square and set the an aspect ratio on the width and height.
However if you want to resize the UIImage depending on the size of the imageView then in your custom UICollectionViewCell make the necessary adjustments in the layoutSubviews(). Here you can inspect the size of the
UIImageView
.

Related

How to preserve UIImageView's width:height ratio when changing width or height

As the title says, I'm looking for a way to preserve the ration of width:height when one of those two changes.
If you set the image of the UIImageView, the frame gets set accordingly. Say the image is 10x10, so the imageView has a 10x10 frame. If you now shrink the width of the imageView to be 5 wide, the image can appropriately scale to be 5x5, however, the imageView becomes a 5x10 imageView, adding transparent blocks above and below the image.
Currently I've set a height constraint that mimics the ratio of the image I want to set, however, if the image is ever updated or changed to one with a different ratio, it will mess up how the image is displayed.
Is there a generic way to tell the imageView to automatically scale it's width and height to be the same original ratio of the image, or is there any property on UIImageView to allow it to preserve it's width:height ratio when changing only one of the properties?
Use aspect ratio Autolayout constraint.
Set Mode to Aspect Fill.
Check Clip subviews.

How to make a UIImageView take up a fixed percent of the screen regardless of device?

Assume I have one big UIImageView that is set to a vector, and I want this ImageView to take up 90% of the screens width while maintaining it's aspect ratio, regardless of device, since it is a vector, resizing is not an issue. Height does not matter since I need it to just maintain it's aspect ratio, so long as it is 90% of the width of the screen. How can I do this in UIBuilder?
Here is how to make an image width 90% of its superview:
Add the UIImageView to the Storyboard.
control-drag from the imageView to the background view.
Select Equal Widths from the pop-up.
Change the multiplier for the constraint to 0.9.
To set up the aspect ratio, control-drag from the UIImageView to itself and select Aspect Ratio from the pop-up. Set the multiplier value of the resulting constraint to the desired value. You can use a ratio such as 3:4 or a floating point value such as 0.75.
In the Attributes Inspector set the Content Mode of the UIImageView to Scale To Fill, Aspect Fit, or Aspect Fill so that the UIImage fills the UIImageView.

Why is this object not positioned properly with Auto Layout?

I place an image view (content size: aspect fit) on my base layout. I create the trailing, leading and bottom constraints and set their constants to 0.
This doesn't happen when the image is smaller than the view:
Though, the image isn't positioned properly on the iPhone - it appears almost centered - while it is on the iPad. Perhaps its intrinsic content size is too big? Why is this happening?
As you haven't set a top, aspect ratio, or height constraint on your image view, iOS falls back to the intrinsic content size (the actual size of the image) for the height only (the width is set by the leading/trailing constraints). If the image is wider than the screen, it will result in an image view that is as tall as the original image, but with the with set to the screen width.
Then, as you have the content mode set to Aspect Fit, iOS places your resized image inside the image view, and leaves lots of blank space around it.
One option to resolve this is to add an aspect ratio contraint on the image view, matching the aspect ratio of the image inside it. This will result in a correct height for the image view.
beyowulf was correct i believe. You should try changing the background color of imageview. If you change the imageview background color, you will realise that imageview always obeys your auto-layout constraint.
Now why not image??
Its because you asked it not to :) aspect fit will try to resize the image still maintaining the aspect ratio :) when the size of the image is too large i.e greater than the size of imageview frame, image covers the full frame of imageview and maintains the aspect ratio as well. Meaning if width is greater than screen width, image will cover the imageview frame width and takes a corresponding height for that width.
Thats why in first case your image covers full imageview frame where as in second where image size is small covers only space required :)
If on the other hand you want image to cover imageview frame always either fall back to scale to fill or aspect fill based on your requirement.
Happy coding :)

iOS Constraints. How to set image view height ration equal to image ratio by constraints?

I've image view with equal width constraint and aspect fit. I need to resize height to the same ration that image has by using constraints is it possible?
Is now:
I need something like:
Use aspect ratio and it will resize using the same proportion for both width and height

UIViewContentModeScaleAspectFill not maintaining aspect ratio of image

I am using UIImageView in UICollectionViewCell. I have set contentMode to UIViewContentModeScaleAspectFill and Clip subviews to YES.
Still image's aspect ratio is not maintained. It shrinks image width/height.
Please note that I know I can center crop image through code but I am looking for a way to do it without using code to change image size.
Also I can't use UIViewContentModeCenter because real image is much larger than UIImageView size.
For maintaining Aspect Ratio you have to use UIViewContentModeScaleAspectFit instead of UIViewContentModeScaleAspectFill.

Resources