我正在尝试将 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();
这是视频的容器(蓝色):
我不明白为什么视频没有绑定(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/