Fixing imageview frame according to image - ios

I am showing images downloaded from an URL in a table view whose custom cells have UIImageViews.
Now my requirement: I want to fix the height of that imageview and cell according to the height of that image in that cell.

The steps to perform are the following: (I won't write the code for you, just guide)
in heightForRow method you pick the image from the data source
then you learn its size (image.size)
return the value like
size.height+SOME_CONSTANT, where SOME_CONSTANT is the space for
other content

Related

Automatic Resize images in collection view

I used an Image inside collection view(entire cell). Images I'm getting from API. I need to resize width & height of collection view & cell based on image size dynamically. Help me out.

How to add image in ImageView of TableController properly?

I have a TableViewController and custom cell. Inside of the cell i have an ImageView. Xcode 9.4
*I do not have enough reputation - so provide links.
Click for image
I have constraints settled to superview Leading-Trailing: 20, Top-Bottom:10, clip to bound enabled, Scale to fill.
When i launch application, i oversee this:
Click for image
Image shrinks down. If i change to Aspect Fit/Aspect Fill the image becomes smaller(image view is not filled) or zoomed(not all image is available).
How to fix this issue? I do not want to give it specific width/heigh for compatibility.
Maybe i should change my constraints? I have watched several tutorials, where these constraints work, even did step by step, however it does not work as supposed to.
All constraints are settled for ImageView to superView (that is cell itself).
Thanks.
Set estimated height for your cell self.tableView.estimatedRowHeight = CGFloat(44) and also tableView.rowHeight = UITableViewAutomaticDimension so that it will grow according to your content inside. the problem here is with your image your cell getting height from your image you provided if you give fix height for your image your problem would be solved.
to adapt height for imageView according to iPhone and iPad.
First way is to get superView height and give your image height according to that programmatically.
Second you can get current device type and give your image you height to that UIDevice.current.userInterfaceIdiom == .pad. you can't do it in storyboard because you don't have access to superView in custom cell.

ImageView in self-sizing table cell uses height of image before Aspect Fit is applied

I have an ImageView inside of a self-sizing table cell that has a content mode of Aspect Fit.
When the table fully loads the image itself looks great, it resizes to fit within the width of the cell while still keeping the original ratio.
However, the height of the table cell is as large as if the Aspect Fit content mode was never applied to the image view.
The red in this image is the image view and the space above and below the actual image is what I'm trying to avoid.
When I replace Aspect Fit with Aspect Fill, this is what the same cell looks like:
This is what I mean when I say it looks like the image view is using the height of the image before Aspect Fit is applied.
Am I missing a constraint or setting in the ImageView?
Any help is appreciated!
If the images are being downloaded from a server, they will take a second or a millisecond to be downloaded. The time they take to download is noticeable to the user. The best user experience is explicitly setting the height and width before the image is downloaded. If you are using a third party API to fetch the image (i.e. facebook or youtube API), the return will include the image URL along with the height and width for the image. This way you can set the height and width of the cell and then wait for the image to download without any interruption or change in the size of cells. This would be the best way to do it. By having the height and width set explicitly, you don't need to worry as much about the content mode to use for the image view. I would only recommend using a self sizing UITableViewCell if you are explicitly setting the height and width of image view within that cell. I hope this helps!
I just saw your most recent comment. You can adjust the size of each image view within each cell depending on the image that is being returned. You don't need to set a fixed height for the image view. You can change the height of each cell depending on the image that is in that cell. I would just recommend doing this before the image is downloaded.
If you are using storyboard, then one way to do this is creating an outlet for the height constraint of the image view.

How to achieve resizing of UITableView cells like instagram?

I need to be able to resize an UIImageView according to the size of the image which is in aspect fit.
and also i need to be able to resize the cell that the image is contained in.
A example would be the instagram app, they have different size images yet the cells are sized appropriately?
Use auto layout when creating your table view cells
Set the table view rowHeight to equalUITableViewAutomaticDimension.
Set theestimatedRowHeight or implement the height estimation delegate method.
You need to adopt the dynamic cell height. Now that cell will automatically determine its height from its content view subviews, make sure you have set your auto layout constraints carefully. Set content mode of UIImageView to aspect fit.
Below are some links to follow:
1. Rey wenderlich
Site Point

Change UITableViewCell Height after creation

I'm working on an app which uses tableviews which displays content fetched from a webservice. In some cells I may need to display an image with unknown dimensions. To do this, I have an UIImageView inside the cell of a fixed width (292pts). I then adjust the imageview height accordingly, like this.
imageviewheight = 292/imagewidth*imageheight
For example, if I have an image that is 730*1250 I set the height to 500 to maintain the aspect ratio and show the full image.
However, this can be a problem when I need to set the cell height. Since the images are downloaded from the internet, the image may not have finished downloading when I need to create the cell. Thus, I cannot determine the cell height until the images have downloaded, and I can't have my UI wait for that, so I display a placeholder image instead, and the cell height is based off the height of that.
Once my image has downloaded I need to display it in the image view. That would be fine, if the height matched the height of the placeholder image. Otherwise, I need to adjust the imageview height to compensate, and in turn change the cell height as well. But, the problem is I can't change the height once the cell has been created.
The only solution that I can think of is saving the UIImage, and reloading the cell. Then, since I already have the image, I can setup the cell height accordingly based off the image. However this seems like a very inefficient solution. Is there any way to adjust the cell height after it has been created? I tried changing the cell's frame, but that didn't do anything. Is there a way to change the cell height after creation, or is reloading the only way?
Once the image is loaded, be sure you're returning the correct new height for the row from your UITableViewDelegate in the method - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath. You may need to call - (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation to force it to re-check the value for the height of that row.

Resources