将 UITableViewCell 高度设置为 UIImage 高度?

Set UITableViewCell height to UIImage height?

我有一个自定义的 TableViewCell,我希望它的高度与 UIImage 的大小相匹配。 Cell 中的 UIImageView 设置为宽高比,我希望 UIImageview 与其包含的 UIImage 一样高,并且宽度等于 table 视图的宽度(屏幕宽度)。在 heightForRowAtIndexPath: 方法中,我有给定 indexPath.row 处的单元格包含的图像。单元格是这样配置的,如果我将 heightForRowAtIndexPath 设置为 500,imageview 是唯一会被拉伸的视图。

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        let image = comics[indexPath.row].image

        return image.size.height + 103
}

103 是构成单元格其余部分高度(按钮和标签)的点(见截图)。

现在的问题是:所有单元格都太高了!好像这还不够奇怪,其中一些太短了??? (图片)我已经阅读了很多类似的帖子,但其中 none 回答了我的问题。 不知何故 image.size.height 比 imageview 中的实际图像大? 我只希望 uiimageview 在 AspectFit 模式下与其中的图像大小相同。

你对此有什么想法吗?如果您还有任何问题,请问我! 谢谢! Screenshot

非常感谢你们!宽高比的东西做到了:

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        let image = comics[indexPath.row].image
        let aspectRatioImg = image.size.height / image.size.width

        return view.bounds.width * aspectRatioImg + 103
    }