maintaining aspect ratio on launch screen - ios

I expect there's a simple answer here. If so, sorry and thanks.
I'm trying to make my launch screen for all the different iPad and iPhone sizes. On the launch storyboard I have two UIViews, one full size for the background (backView) and one in the shape of a square (squareView). I'm trying to keep the square from stretching (i.e. I'm trying to maintain a 1:1 aspect ratio) and make it 0.75 the width of whatever device the app runs on. I know that there will be varying amounts of space below, but how can I keep the square from distorting when I switch devices?
I've been researching the constraints and diddling with them for hours. I expect someone will tell me it's easy, just do ... what?

First, I've centered my rect horizontally and vertically
Then I've set the rect's width to x0.75 of his superview
For the final step I'm setting his ratio to 1:1
That's it! Final result:

Related

XCode12: How to set up constraints in an image to make it responsive to an screen?

I'm having issues with constraints in XCode 12: Even if I describe accurately all measures involved, there is no way apparently to resize a 850x600 image to make it fit proportionally in the screen of an apple device. As you can see in the image, the constraints are defined to make the original image fit WITH BORDERS on the Iphone's screen, but nevertheless is turns out to be a disaster afterwards...
As you can see in the screen capture, the image overflows the screen when the model device is changed. So...we have a problem. And the answer or clue that I'm seeking night and day is...
How to make this image to become RESPONSIVE to screen size, and keep it's proportions. No way how the hell something so basic is so difficult in XCode...
must edit... look how the alignment constrainst are shown in grey...no idea why...this avoids me to try the solution provided by the good fellow above...
It seems you've defined constraints to center the imageView and having it a size of 850x600 pixels. This will keep the image of that exact same size, regardless the device it is shown on.
If you want the image to shrink, keeping the aspect ration you need to define constraint for the top, bottom, leading and trailing edge.
And you need to set the imageView's contentMode to "Aspect Fit", which is the default, afaik.
This is an image shown on an iPhone 11:
And this the same image shown on an iPhone 4s:
Edit:
One way to add those constraints is to select (highlight) the imageView and click on the button with the little square in the middle surrounded by those "T"s. :-)

How can I use UIImageView with aspectFill and alignment to the top?

How could I use UIImageView with aspectFill and alignment to the top? Auto-layout or programmatically don't mind.
It can be done, but you are talking about quite a bit of coding. Let's start with a detailed description of what you are asking for.
We'll start with an iPhone6s in portrait mode with a screen size of 667x375 points, or a regular height by compressed width in size classes.
(Keep in mind, on any iPad you'll also need to work with slide-over and split screen too.)
But for now we'll go with 667x375. Into that we'll add our imageView as a square size of 375x375. Along the top of the screen when in portrait mode.
(Keep in mind that with AutoLayout you'll also have it pinned, and without putting some code in to determine if it's in landscape or portrait, otherwise, that 375x375 will shrink in landscape if you have other views underneath it.)
So now, stripped out to the most minimum (for now), we are talking about how to pin "an image" to the top of a UIImageView that is 375x375 in size and with a contentMode of aspectFill.
We need to find the size of the image next, based on both the largest dimension (height or width) AND how that dimension compares to the size of the imageView, calculate how the imageView will normally render it. The key figure you want is the height it will render it as
For instance, how will a 480x1020 (HxW) image fit into it? The 1020 width will be taken down to 375, so the height will be 480/(1020375) or 176.4705 points.
Always do the math for both dimensions - if your imageView isn't square or if the height isn't the "bounded" dimension, you'll be off.
Once you've calculated what the "Aspect Filled" height will be, check it against the dimensions of the imageView, and adjust the height of it as needed.
Here's where the real work comes in. If you are using AutoLayout, you can do a heightAnchor adjustment, but depending on how you pinned those other views things may be laid out really bad after you do. If you are using frames and CGRects be aware that you have to take into account iPad slide-out and split screen, etc.
Finally, if you want your app to load a different image in that imageView after it was loaded, you will have to "reset" the height back to what it originally was, at least in your calculations.
My recommendation is to find an alternative to what you are asking. For instance, put a small border around the image view to let the user know the true dimensions of that view.

How to properly constrain images in interface builder

I'm new to iOS development and am struggling with interface builder to layout a view that has 3 images. I want three images going across the view. So the images will need to be scaled a little differently for different devices. I am hoping to do this with constraints. I am fairly close. Here is a screenshot:
The images are actually resizing and becoming bigger on the larger device screens which is what I want and the spacing is working to stop them from overlapping each other on small devices. I just need to stop the two side images from being stretched tall. I don't want to specify a height constraint because they do need to resize for specific devices sizes. Any help to add a constraint that will make the outer images not stretch tall is much appreciated.
Set the UIImageView 's content mode to be aspect fit. It will be always stretched in it's correct aspect ratio
Instead of Scale to Fill select Aspect Fit

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

iPhone gallery app crops photo to 3:2 from 4:3 aspect ratio, how?

The photo taken using the UIImagePickerController is of 4:3 aspect ratio. However, the full screen aspect ratio is 3:2. So the gallery app is doing some magic to show the photo as 3:2 aspect ratio. When you zoom out in the full screen view, the photo appears in 4:3 aspect ratio. Can anyone shed light on how it could be done? I've been breaking my head for the past two weeks on this.
Really appreciate the help!!
To fit a 4:3 image into a 3:2 space you can either match the height or match the width.
If you were to match the height then you'd turn the 3 in 4:3 into the 2 in 3:2. So you'd scale the entire image by 2/3. Since you'd be scaling width and height by the same amount, the effective height after scaling would be the 4 from 4:3 scaled by 2/3, to give 8/3 — a bit less than three. You'd therefore not quite fill the screen.
Conversely, if you were to match the width then you'd turn the 4 in 4:3 into the 3 in 3:2. So you'd scale the entire image by 3/4. Since you'd be scaling width and height by the same amount, the effective height at the end would be the 3 from 4:3 scaled by 3/4, to give 9/4 — a bit more than two. You'll therefore slightly more than fill the screen.
So that the photos app does is display pictures with an initial zoom so as to fit the width of the stored image to the width of the display. If the stored image is 3264x2448 (which I think it is on the iPhone 4S and the 5) then on an iPhone 4s — using points rather than pixels — it's scaled by a ratio of 480/3264. If you work that out, it gives the image a final height of very close to 360pt, 40pt wider than the screen.
In terms of UIKit, that probably means putting a UIImage inside a UIScrollView and setting the initial value of zoomScale to 480/3264 (ie, approximately 0.15). The scroll view can help you with zooming in and out though there's still some manual work to be done — see e.g. this tutorial. By setting a minimumZoomScale of 320/2448 (ie, approximately 0.13) you'll automatically get the behaviour where zooming out as far as you can go ends up showing the entire 4:3 image on screen.
not sure how you obtain your image, but you might have gotten one of the representations of the image. One of those representations is specifically for getting a quick fullScreen CGImage, an other will return the FullResolution. FullScreen will be whatever is needed for the device (640x960 on iPhone4), Full resolution would be the 8MP picture.

Resources