简体   繁体   English

如何自动调整 AVPlayerLayer 的大小以在 Swift 的整个父视图上以全尺寸显示视频

[英]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.

相关问题 Swift:调整AVPlayerLayer的大小以匹配其父容器的边界 - Swift: Resize an AVPlayerLayer to match the bounds of its parent container Swift:如何在AVPlayerLayer的顶部添加图片/文本,使其成为视频的一部分? - Swift: How do I add an image/text on top of my AVPlayerLayer for it to be a part of the video? iOS自动布局-当您想使视图拉伸到其父视图的完整大小时,正确的方法是什么? - iOS auto layout - when you want to make a view stretch to the full size of its parent, what is the correct way to go? Swift:使用父视图大小动态调整/缩放UIBezierPath - Swift: Resize/Scale UIBezierPath dynamically with parent view size SWIFT:如何以编程方式设置集合视图的框架大小,使其等于其父视图? - SWIFT: How to set the frame size of a collection view programmatically so that it's equal to its parent view? 如何自动调整 UIScrollView 的大小以适合其内容 - How do I auto size a UIScrollView to fit its content 在 IB 中,当我调整父视图的大小时,如何让子视图保持与父视图相同的大小? - in IB how do I get a subview to stay at the same size as it's parent when I resize the parent? Swift-如何根据视频播放器框架的大小动态调整UIView的大小 - Swift - how to resize a UIView dynamically based on the size of a video player frame 如何将UIStackView约束到其父视图的中心? - How do I constrain a UIStackView to the center of its parent view? 在Swift中的HeightForRowAt函数中设置UITableViewCell的大小后如何调整其大小? - How to resize UITableViewCell after setting its size in HeightForRowAt func in Swift?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM