【问题标题】:Swift -how to make a smaller version of a 16:9 video thumbnail but keep the aspect ratio the sameSwift - 如何制作更小的 16:9 视频缩略图但保持纵横比相同
【发布时间】:2020-05-12 10:58:30
【问题描述】:

1080p 高清视频的宽高比为 16:9,并为单元格中的视频设置帧,以便将其完全填满我会使用view.frame.width * 9 / 16

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

    // the collection view is pinned to both sides of the vc with no spacing
    let width = collectionView.frame.width

    let videoHeight: CGFloat = width * 9 / 16

    return CGSize(width: width, height: videoHeight)
}

// inside the cell itself:

let videoHeight: CGFloat = self.frame.width * 9 / 16

contentView.addSubview(thumbnailImageView)
thumbnailImageView.topAnchor.constraint(equalTo: contentView.bottomAnchor).isActive = true
thumbnailImageView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor).isActive = true
thumbnailImageView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor).isActive = true

// here is where I set the thumbnail's height
thumbnailImageView.heightAnchor.constraint(equalToConstant: videoHeight).isActive = true

这完美地给了我:

问题是我想要一个类似于 youtube 的小版本的 thumbnailImageView。我想在屏幕左侧添加较小的 thumbnailImageView,但 保持纵横比不变

问题是当我尝试这样做时,我得到的是正方形而不是矩形。我将单元格的宽度除以 3,然后将其乘以 9 /16,但这不起作用。我使用了(width / 3),因为我虽然它会保持纵横比不变,但会减小 thumbnailImageView 的大小,但它不起作用。

我哪里出错了?

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

    // the collection view is pinned to both sides of the vc with no spacing
    let width = collectionView.frame.width

    let videoHeight: CGFloat = (width / 3) * 9 / 16

    return CGSize(width: width, height: videoHeight)
}

// inside the cell itself
let videoHeight = (self.frame.width / 3) * 9 / 16

contentView.addSubview(thumbnailImageView)
thumbnailImageView.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 8).isActive = true
thumbnailImageView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 8).isActive = true

// here is where I set the thumbnail's height and width
thumbnailImageView.heightAnchor.constraint(equalToConstant: videoHeight).isActive = true
thumbnailImageView.widthAnchor.constraint(equalToConstant: videoHeight).isActive = true

【问题讨论】:

  • 问题在于算术。要在保持长宽比不变的情况下缩放矩形,您必须将其高度和宽度乘以(或除)相同的数字
  • @matt 我应该把它除以多少?
  • 任何你喜欢的号码。

标签: ios swift cgsize


【解决方案1】:

您可以使用 AVMakeRect(aspectRatio:insideRect:) 函数来获取一个缩放的矩形,该矩形在边界矩形内保持指定的纵横比。

您可以参考APIhere

通过使用上述 API,您的 videoHeight 值将填充如下。

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    // the collection view is pinned to both sides of the vc with no spacing
    let width = collectionView.frame.width
    let rect = AVMakeRect(aspectRatio: CGSize(width: 16, height: 9), insideRect: CGRect(origin: .zero, size: CGSize(width: width / 3, height: CGFloat.infinity)))
    let videoHeight: CGFloat = rect.size.height
    return CGSize(width: width, height: videoHeight)
}

在单元格中,您可以简单地具有如下高度。

let videoHeight = self.frame.height

【讨论】:

  • 我以前从未使用过该功能,我现在根据这个答案尝试它:stackoverflow.com/a/37996898/4833705。给我几分钟
  • 它仍然是一个正方形,但 cmets 中的@matt 说问题是我正在使用的数学方程。让我先弄清楚那部分,然后将其添加到您发布的方法中。
  • 您可以使用更新答案中提供的 sn-p 检查相同的内容吗?并且还要删除函数中的 videoHeight 定义。
  • 我做错了什么。我使用 self.frame.width 和 sizeForItem 添加了您发布到两个单元格的代码 sn-p 我使用了 collectionView.frame.width (cv 固定在 vc 的两侧)。如果我不使用 videoHeight 那么我将无法设置单元格的高度。当我添加它时,我仍然有一个正方形
  • 在 sizeForItem 里面应该是 collectionView.frame.width / 3。你能记录你得到的框架和高度吗?
猜你喜欢
  • 2019-03-19
  • 1970-01-01
  • 2020-01-03
  • 1970-01-01
  • 1970-01-01
  • 2015-05-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多