首页 > 解决方案 > 如何使用 AVFoundation 制作视频填充帧

问题描述

斯威夫特的初学者。我的视频没有填满它的框架。这是我的代码。我需要实施什么才能占用我在 Storyboard 中设置的空间?

导入 UIKit 导入 AVFoundation 导入 AVKit

类视图控制器:UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

扩展 ViewController: UITableViewDelegate, UITableViewDataSource {

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return 1
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "cellview", for: indexPath) as! DataCellClass

    let videourl = URL(fileURLWithPath: Bundle.main.path(forResource: "sample", ofType: "mp4")!)

    let avPlayer = AVPlayer(url: videourl as URL)

    cell.PlayerView?.playerLayer.player = avPlayer

    cell.PlayerView.player?.play()

    return cell
}

}

标签: swiftavfoundation

解决方案


尝试在 Storyboard 中的 DataCellClass 中查看 AVPlayer 的内容模式。确保将其设置为 Aspect Fill

这可能会有所帮助。https://useyourloaf.com/blog/stretching-redrawing-and-positioning-with-contentmode/


推荐阅读