[英]How do I auto resize an AVPlayerLayer to show the video in its full size on the whole parent view in Swift
Im trying to display a video using the AVPlayerLayer
on top of a UIImage
.我试图在
UIImage
上使用AVPlayerLayer
显示视频。 The video is supposed to resize itself so that the full video is shown on the image view, even if it has to stretch itself to do it.视频应该自行调整大小,以便在图像视图上显示完整的视频,即使它必须拉伸自己才能做到这一点。 Unfortunately I dont get those results, my video end up looking like this instead on the simulator.
不幸的是,我没有得到这些结果,我的视频最终在模拟器上看起来像这样。 Image of the results
结果图片
Ive tried setting the AVPlayerLayer.frame manual to the image views size, to demonstrate what im aiming for it to look like on all the diffrent iPhone sizes.我尝试将 AVPlayerLayer.frame 手册设置为图像视图大小,以展示我的目标是让它在所有不同的 iPhone 尺寸上看起来像什么。 Image of the goal results
目标结果的图像
Here is my code:这是我的代码:
let playerLayer = AVPlayerLayer(player: queuePlayer)
playerLayer.frame = feedBackground.bounds
playerLayer.videoGravity = .resizeAspectFill
feedBackground.layer.masksToBounds = true
feedBackground.layer.addSublayer(playerLayer)
(feedBackground is a UIImage) (feedBackground 是一个 UIImage)
dont know if its important but feedBackground's contentMode is scale to fill不知道它是否重要,但 feedBackground 的 contentMode 是按比例填充的
Im quite new to app development so thanks for taking you time to read my question and thanks for any solutions you may have:)我对应用程序开发很陌生,所以感谢您抽出时间阅读我的问题,并感谢您提供的任何解决方案:)
Well, i found out a solution.好吧,我找到了解决方案。 I was integrating the video in viewDidLoad and was setting the playerLayer.frame to be equal to feedBackground.bounds - i think the reason why it wasn't working was bcs the graphics hadn't loaded yet so i couldn't set my videos frame to be equal to feedBackgrounds frame bcs feedBackgrounds frame wasn't calculated / made yet.
我将视频集成到 viewDidLoad 中,并将 playerLayer.frame 设置为等于 feedBackground.bounds - 我认为它不起作用的原因是图形尚未加载,所以我无法设置我的视频帧等于 feedBackgrounds 框架 bcs feedBackgrounds 框架尚未计算/制作。
Maybe am wrong, either way it worked by moving my code to viewDidAppear or viewWillAppear.也许是错的,无论是通过将我的代码移动到 viewDidAppear 或 viewWillAppear 来实现的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.