Resizing UIImageView Maintaining Aspect Ratio - ios

I want to resize the UIImageView and keeping its aspect ratio.
What have I done is just using Center Horizontally and Vertically constraints on storyboard.
If let say I using top and bottom, leading and trailing constraints, it doesn't keep the frame same as image ratio. The red border represents UIImageView frame.
The result will be like this if the image is in suitable size
However, the frame sometimes will be out of view range, but the ratio is maintained with image.
I have done searching everywhere and mostly ended with suggesting with UIView.ContentMode.scaleAspectFill, which does not fulfill my needs.

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.

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 :)

How do I get a square image to scale up evenly if the phone screen size increases?

I am having trouble figuring out how to set up the constraints on an image view.
I have an imageView of size 155*155. I want the view to remain a square shape but scale up if the iPhone screen size increases to 6 or 6+. I can constrain the distance to the bottom of the container and the sides of the imageView to the sides of the main view. This should stretch the width but is there a way to make the height increase by the same amount? Any pointers would be really appreciated. Thanks
You can use an aspect ratio constraint by ctrl + dragging from your image INTO your image.
This basically tells that you want to maintain the current aspect ratio as your other constraints force a change in the width or in the height.

Xcode 6 UIImageView will not scale correctly

Please see screen shot of flower setup below. The flower image has been correctly loaded from an asset catalog and when the app is run on various simulators the correct pixel resolution is assigned to each device. My problem is how to get the flower image to be scaled (equally sized to fit) the same on each device ??
I have learnt how to position the image to different positions using constraints and frames but the image never scales correctly - please see first pic
The following image is a mock up of what I want to be able to do (flower image scaled correctly on each device)
Judging by your mockups, it looks like you want the image to fill half the width, and keep its square aspect ratio to determine its height. One way to approach this would be to use AutoLayout to make a left UIImageView and a right placeholder (blank) view. Pin the left view to the left edge of the parent, the right view to the right edge, and then set them to be 0 pixels from each other. Then set an equal widths constraint on them. Finally, control drag the image view to itself and you can select aspect ratio -- and assuming that in IB, the width and height is the same, it will keep it square. Adding an equal heights constraint will give the other view the height it needs to be equal in case you need that.
This gives you a left image view that is 50% and with your mode set to Aspect Fit or Aspect Fill, it should give you the results in your mockups. In case you have an image that isn't square, make sure to check Clip Subviews for your UIImageView to prevent showing the overflow.
The real problem is that your goals are not well defined. Scaled with respect to what? The screen has a height and a width. You can't scale with respect to both, because that would distort the image (because different devices have different overall aspect ratios). Thus you have to pick one dimension that you will scale to.
Judging from your mockup, I'm going to guess that what you want is to scale with respect to height. If so, then give the image view a height constraint that is a fixed fraction of its superview's height. It looks to be about 0.25 in your mockups but you will have to eyeball what you think is a good fraction.
Now, if the content mode of the image view is Aspect Fit (or Aspect Fill), the image will change its height along with the image view without distorting the aspect ratio.
However, it would be best if you would make the other dimension (here, the width) of the image view a fixed fraction of the height, such as to make the aspect ratio of the image view the same as the aspect ratio of the image. The reason is that otherwise the image might end up centered in the image view in such a way that it doesn't touch the top or left any more, even though the image view itself does.
CGFloat screen_width = [[UIScreen mainScreen] bounds].size.width;
your_imageview.frame = CGRectMake(0, 0, screen_width/2, screen_width/2);

UIViewContentModeScaleAspectFit always centers the UIImageView, either vertically or horizontally. Can it not?

Pretty simple question. UIViewContentModeScaleAspectFit always centers the UIImageView in its frame either vertically or horizontally. Is it possible to offset that?
Say it's aligned in the vertical center, I might want it at the vertical top. It would still fit to its aspect.
The simplest way to achieve this is to use a container view for the image view, and move your image view within the container.
You would still need to aspect fit the image, but you would need some additional logic to alter the frame of your image view depending on the aspect ratio of the image itself, and the size of the frame you are fitting it into. You can get this from the size property of the image.

Resources