8

我在 UITableViewCell 中有一个图像视图。

如果图像很大,带有 Aspect Fit 的 Imageview 会在顶部和底部创建大量额外空间。额外的空间(灰色背景颜色)如下图所示。

如何删除多余的空间?

Imageview 额外空间
几点注意事项:

  1. 这张特殊的图片是 890 x 589,屏幕截图是在 iphone 6S 模拟器上拍摄的。

  2. 我正在使用 UITableViewAutomaticDimension 自动表格单元格高度。

  3. imageview 没有高度限制,因此它可以相应地调整大小。

先感谢您。

4

2 回答 2

5

我遇到过同样的问题。当原始图像大小(宽度和/或高度)大于保存它的 UIImageView 并且使用 Aspect Fit 调整图像大小以适合 UIImageView 时,会发生这种情况。

在这里,图像宽度大于 UIImageView 的大小,因此 Aspect Fit 调整图像大小,保持原始纵横比。如果您读取图像大小(通过手表或断点或其他方式),您将看到图像大小(例如)600(h)x 800(w)(3:4 比例)。UIImageView 宽度为(例如)320(w),因此显示的图像缩放为 240(h) x 320(w)(保持 3:4 的比例)。但是图像宽度仍然是 600 x 800,并且由于 UIImageView 高度没有限制,因此 UIImageView 大小为 600 x 320 -->它将 UIImageView 高度设置为原始图像高度(600),因为它可以。

我的解决方案是这样的:(Swift 3)

向 Main.storyboard 中的 UIImageView 添加高度约束,并通过 Outlet 链接它:

@IBOutlet weak var displayimage: UIImageView!
@IBOutlet weak var displayimageHeightConstraint: NSLayoutConstraint!

然后测试 UIImageView 是否比它持有的图像小(宽度)。如果是这样,设置高度约束,使 UIImageView 高度:宽度比与图像纵横比相同:

if displayimage.frame.size.width < (displayimage.image?.size.width)! {
    displayimageHeightConstraint.constant = displayimage.frame.size.width / (displayimage.image?.size.width)! * (displayimage.image?.size.height)!
}

如果由于 UIImageView 高度受限且宽度设置为原始图像宽度的调整大小问题导致图像两侧存在空白,则也可以进行互补操作。

@IBOutlet weak var displayimageWidthConstraint: NSLayoutConstraint!

if displayimage.frame.size.height < (displayimage.image?.size.height)! {
    displayimageWidthConstraint.constant = displayimage.frame.size.height / (displayimage.image?.size.height)! * (displayimage.image?.size.height)!
}
于 2017-03-02T01:14:05.087 回答
1

使用 UITableViewAutomaticDimension 时,上述答案不起作用。

一般来说,问题是在tableView函数“cellForRowAt indexPath”中,tableView还没有布置单元格,它是子视图。

所以解决这个问题的一种方法是使用 UITableView 的子类:

class MyUITableView: UITableView {

    // Makes real device cell width available in "cellForRowAt indexPath"
    override func dequeueReusableCell(withIdentifier identifier: String, for indexPath: IndexPath) -> UITableViewCell {
        let cell = super.dequeueReusableCell(withIdentifier: identifier, for: indexPath)
        cell.frame.size.width = self.frame.size.width
        cell.layoutIfNeeded()
        return cell
    }
}

现在您可以使用任何解决方案,例如 Md Rais 评论中的链接。例如我自己的代码:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! ViewCell

    if let attach = eventsByDay[indexPath.section][indexPath.row].attachPhoto {
        let cellWidth = cell.attachPhoto.frame.size.width
        let scale = cellWidth / attach.size.width
        let size = CGSize(width: cellWidth, height: attach.size.height * scale)

        UIGraphicsBeginImageContextWithOptions(size, true, 0.0)             
        attach.draw(in: CGRect(origin: CGPoint.zero, size: size))
        cell.attachPhoto.image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
    } else {
        cell.attachPhoto.image = nil
    }

    // ...
    return cell
}
于 2018-06-02T21:16:33.123 回答