【发布时间】:2020-05-15 10:01:58
【问题描述】:
如果我想在单元格内显示 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 个单元格,这只是纵向单元格示例的屏幕截图):
我知道如何设置单元格,但我不知道要为纵向视频使用什么尺寸:
比如我查到if the video is portrait:
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)
纵向视频的尺寸是多少?
【问题讨论】: