A BETTER and cleaner solution, just plays with the height of the cell , allowing you to automatically resize the image using Autolayout .
This is easy:
In your cell add a UIImageView and set the size you want to have when the cell has the actual height;
Set this UIImageView as a constraint, at least at the top, bottom, and one of the side border (right or left) ; also set restrictions on maintaining width / height ;
At this moment, when the cell grows, the image grows in height , and to maintain the proportion (thanks to the restriction), it changes the width , ALWAYS having the correct and proportional size.
Now you just have to write code for cell height:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ static CGFloat cellDefaultHeight = ; static CGFloat screenDefaultHeight = ; CGFloat factor = cellDefaultHeight/screenDefaultHeight return factor * [UIScreen mainScreen].bounds.size.height; }
Obviously set the contentMode in the UIImageView to AspectFill .
source share