UITableView cell auto height adjustment with image scaling issue iOS 9 - ios

I have a UITableView, which has a custom cell that I want to display a single image. I want the cell to automatically adjust the height according to the image's size. The UIImageView is filled in the cell, with auto layout constraints set on the four edges.
The images may be on different sizes, some are wider than the screen width. In such case, due to the content mode I set on the UIImageView, the image will be scaled to aspect fit the entire UIImageView.
When I set everything up and run, I realized that the UITableView didn't set the cell height correctly. It seemed that the image was scaled to fit the UIImageView but UITableView still set the cell height to be equal to the original image's height. Besides this issue, I also don't know why the image was scaled given that there are only 640 pixels on the width of the image and the iphone 6 simulator should have 750 pixels on the width ?
As summary, I have two questions here:
If I give an image with 640 pixels wide to an UIImageView that fills the entire width of the screen, why the image is scaled?
If the image is indeed scaled, how to let the UITableView automatically calculate the cell height based on the scaled size rather than the original size.
If possible, please use Swift instead of Objective-C for showing the code. Also you can assume I am using iOS 8+. Thank you.

Related

Make UIImageView fit width of screen but keep aspect ratio [swift]

I'm trying to build a simple tableview with a tableView showing a list of images people posted and comments their friends added.
This is a subclass of TableViewCell, and I used stack views to lay it out. The problem is that I want to show the photos without stretching them - I want them to keep their original aspect ratio, but people can upload images of any dimensions. You see in the screenshot that the image is squished.
What I noticed is that the larger the uploaded .jpeg file, the taller the cells grew (I'm guessing the image size is dictating the intrinsic height?), but since the width can't change, each image is squished differently. A huge picture will show up super tall but with the same width.
I tried setting the image to Aspect Fit, but only the visible image itself does what I want: fit to width and not warp the aspect ratio. However, the UIImageView itself is still super tall and pushing all other UI elements out of the way.
I must be missing something here... It shouldn't be so hard, should it?
To constraint the image as aspect ratio , first give it width of screen , then give it a static height in IB and hook that constraint as IBOutlet
and set it inside cellForRow
cell.imageHeightcon.constant = imageRealHeight*imageWidthInIB/imageRealWidth
You should fix the size of the image view for whatever you want. Then you should use aspect to fill instead of aspect to fit and select the option clip to bounds in the attribute inspection

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.

iOS Button Vector Image doesn't automatically adjust to screen size

I am currently trying to introduce myself to Xcode, IB and Vector images. I am struggling with the auto layout and having the buttons adjust to screen size.
As you can see the buttons are same size in the iPhone 6s and iPad Pro.
And here is the iPad Pro 12.9" size of the buttons:
The vector images I use are universal .pdf images at the size of 50x41. And here are the settings to those:
How do I get these images to adjust properly from screen size to screen size?
Thanks a lot for your help!
What constraints do you have to change the size of the imageView? I would expect to see a constraint between the top two images (and the bottom two) that forces a constant gap.
The constraints on each image's width should then have a lower priority so they get stretched on a bigger screen. Then you want to constrain the height of each image to its width so they stay 1:1 (I'm guessing), and therefore stretch vertically if they are stretched horizontally.
Set the UIImageView content mode to UIViewContentModeScaleAspctFit and set AutoLayout constraints on the container for aspect ratios rather than just spacing and fixed sizes.

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

crop image in cell uiimageview

Is it possible to trim the bottom of the image that extend beyond the frame. I've got an image height of 280px, and the frame is targeted at 240px, if you compress the height, the image loses its quality.
I turned on the mode - Aspect Fit and enable "Clip Subviews" and also have auto-layout within the maximum image height 240px
In the pictures, the text must be below the pictures, but not inside it.

Resources