如果我想在单元格内显示 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)
纵向视频的尺寸是多少?