gpt4 book ai didi

swift - 如何重新缩放以纵向模式拍摄的视频以适合 UIView?

转载 作者:行者123 更新时间:2023-11-28 05:39:10 28 4
gpt4 key购买 nike

我有一个 UICollectionView feed,用户可以在其中录制视频,视频将在 feed 中显示和播放。如果用户以横向模式拍摄视频,则该视频非常适合 UIView。但是,如果用户以纵向模式拍摄,则两侧会出现白色水平条,填充多余部分。

Given the video URL, is there a way to scale the video to fit the UIView in a cell of the UICollectionView?

旁白:我还想最终在线播放这些视频,这样它们就不会同时播放。是否有一个好的框架可用于引导此功能?

UICollectionView 提要中的单元格将包含一个 UIView,它将容纳视频播放器。这是 UIView 的类 PlayerViewClass:

import Foundation
import UIKit
import AVKit
import AVFoundation

class PlayerViewClass: UIView {

override static var layerClass: AnyClass {
return AVPlayerLayer.self
}

var playerLayer: AVPlayerLayer {

return layer as! AVPlayerLayer
}

var player: AVPlayer? {
get {
return playerLayer.player
}

set {
playerLayer.player = newValue
}
}
}

请注意,单元格 MyCollectionViewCell 有一个链接到此 UIView 的 IBOutlet:

class MyCollectionViewCell {

@IBOutlet weak var playerView: PlayerViewClass!


override func awakeFromNib() {
super.awakeFromNib()

//Setup, not relevant

}

}

FeedViewController 中 Feed 的 collectionView cellForItemAt indexPath 委托(delegate)方法如下:

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)

if let cell = cell as? MyCollectionViewCell {

...

//Configuring the cell

...

//Video player
let avPlayer = AVPlayer(url: post.fullURL)

**//TODO: Scale the video to the playerView (UIView)**

//Setting cell's player
cell.playerView.playerLayer.player = avPlayer

//Play
cell.playerView.player?.play()

}
return cell
}

或者,我可以设置func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { } 所以单元格 View 根据其尺寸适合视频的大小?

如有任何帮助,我们将不胜感激!

最佳答案

您可以使用 AVPlayerLayervideoGravity 属性。将其设置为 resizeAspectFill,这样视频将填满单元格,但视频的两边会被剪切,因此请确保它适合您。

关于swift - 如何重新缩放以纵向模式拍摄的视频以适合 UIView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57552161/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com