【问题标题】:Swift -Frame dimensions for portrait videoSwift - 纵向视频的帧尺寸
【发布时间】: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)

纵向视频的尺寸是多少?

【问题讨论】:

    标签: ios swift cgsize


    【解决方案1】:

    使用此blog post 表示 iPhone 垂直纵横比(纵向视频的尺寸)为 9:16 和此 answer 我为 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)
    } 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-12
      • 2019-06-22
      • 1970-01-01
      • 2011-05-06
      • 2023-04-08
      • 1970-01-01
      • 2013-03-04
      • 1970-01-01
      相关资源
      最近更新 更多