0

如果我想在单元格内显示 16:9 水平视频,请使用以下尺寸:

let videoHeight = self.frame.size.width * 9 / 16

contentView.addSubview(thumbnailImageView)
thumbnailImageView.heightAnchor.constraint(equalToConstant: videoHeight).isActive = true
thumbnailImageView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor).isActive = true
thumbnailImageView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor).isActive = true

// In the cv's sizeForItem I set the same height
let width = collectionView.frame.width
let videoHeight: CGFloat = width * 9 / 16
return CGSize(width: width, height: videoHeight)

这会给我一个水平单元格:

在此处输入图像描述

如果视频是 16:9,那么这项工作很好,但如果用户以纵向录制视频,则视频缩略图位于视图的中间,两侧为黑色。

在这种情况下,当它是肖像视频时,我想调整单元格以使其支持肖像视频,从而为我提供类似于所有社交网络当前用于肖像的东西(我只需要 1 个单元格,这只是肖像单元格示例的屏幕截图):

在此处输入图像描述

我知道如何设置单元格,但我不知道肖像视频使用什么尺寸:

例如,我发现视频是否是纵向的

guard let videoTrack = AVAsset(url: videoUrl).tracks(withMediaType: AVMediaType.video).first else { return }
let transformedVideoSize = videoTrack.naturalSize.applying(videoTrack.preferredTransform)
let videoIsPortrait = abs(transformedVideoSize.width) < abs(transformedVideoSize.height)

var videoHeight = self.frame.size.width * 9 / 16 // dimension for horizontal video
if videoIsPortrait {

    videoHeight = // dimension for portrait video ???
    videoWidth = // not sure if this is necessary
}

thumbnailImageView.heightAnchor.constraint(equalToConstant: videoHeight).isActive = true
thumbnailImageView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor).isActive = true
// if a width is necessary I set it otherwise I just use the trailing anchor

// I do the same thing inside sizeForItem to adjust to the portrait height (and width if necessary)

纵向视频的尺寸是多少?

4

1 回答 1

0

使用这篇博客文章说 iPhone 垂直纵横比(纵向视频的尺寸)是9:16,这个答案我做了 2 列:

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

    let widthForTwoColumnCells = collectionView.frame.width / 2

    let insideRect = CGRect(x: 0, y: 0, width: widthForTwoColumnCells, height: .infinity)
    let rect = AVMakeRect(aspectRatio: CGSize(width: 9, height: 16), insideRect: insideRect)

    let videoHeight: CGFloat = rect.size.height

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

在此处输入图像描述

于 2020-05-30T09:42:14.167 回答