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