iphone - AVPlayerLayer 未绑定(bind)到 UIView 框架

标签 iphone xcode swift avplayerlayer cglayer

我正在尝试将 AVPlayerLayer 添加到 UIView

    self.player = AVPlayer(URL: NSURL(fileURLWithPath: path!))
    self.playerLayer = AVPlayerLayer(player: player);
    self.playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
    self.playerLayer.frame =  ctrVideo.bounds;
    self.ctrVideo.layer.addSublayer(playerLayer);
    player.play();

这是视频的容器(蓝色):

This is the view:

约束: Constraints:

最终结果: enter image description here

我不明白为什么视频没有绑定(bind)到 UIVIew 坐标。如果我将它绑定(bind)到 Controller 的 super View 中,就可以了。

最佳答案

当 View 的框架改变时,子图层不会自动调整大小,您必须手动调整。您可以子类化 UIView 以使其更容易。

class VideoContainerView: UIView {
  var playerLayer: CALayer?
  override func layoutSublayersOfLayer(layer: CALayer) {
    super.layoutSublayersOfLayer(layer)
    playerLayer?.frame = self.bounds
  }
}

然后在你的代码中:

self.player = AVPlayer(URL: NSURL(fileURLWithPath: path!))
self.playerLayer = AVPlayerLayer(player: player);
self.playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
self.playerLayer.frame =  ctrVideo.bounds;
self.ctrVideo.layer.addSublayer(playerLayer);
self.ctrVideo.playerLayer = playerLayer;
player.play();

只需确保将“ctrVideo”的类从 UIView 更改为 VideoContainerView。

关于iphone - AVPlayerLayer 未绑定(bind)到 UIView 框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35443402/

相关文章:

iphone - 多行标签

objective-c - 升级到 Xcode 7.3 swift 2.2 后,从 swift 调用 obj-c 枚举不起作用

ios - 复制数组中已有的项目

iphone - iPhone 中的 cookie 和 session

iphone - SQLite 可以在 iPhone 上存储多少?

使用 Core Graphics 的 iPhone 光泽图标

.net - 使用 CCKeyDerivationPBKDF 获取派生 key

objective-c - 如何将 Scintilla 包含在 XCode 3.2.5 项目中?

swift - Alamofire 导入失败

ios - Swift - 将 nil 核心数据字符串转换为可选值