gpt4 book ai didi

ios - 在 UIScrollView 中调整 AVPlayer 的大小

转载 作者:行者123 更新时间:2023-12-02 00:02:23 25 4
gpt4 key购买 nike

我有一个 AVPlayerViewController,其 AVPlayer View 添加为 UIView 的 subview 。此 UIView 被添加到 UIScrollView 中。这个想法是为视频实现“移动和缩放” View 。当 UIScrollView 内的 View 是 UIImageView 时,我可以使用它。但是,当 View 是带有 AVPlayer subview 的 UIView 时,UIView 的比例显得太小。

这是我的代码:

override func viewDidLoad() {
super.viewDidLoad()

videoPlayerController = VideoPlayerController()
videoPlayerController.videoURL = url
self.addChildViewController(videoPlayerController)
videoView.addSubview(videoPlayerController.view)

videoPlayerController.view.translatesAutoresizingMaskIntoConstraints = false

let left = NSLayoutConstraint(item: videoPlayerController.view, attribute: .left, relatedBy: .equal, toItem: videoView, attribute: .left, multiplier: 1, constant: 0)
let right = NSLayoutConstraint(item: videoPlayerController.view, attribute: .right, relatedBy: .equal, toItem: videoView, attribute: .right, multiplier: 1, constant: 0)
let top = NSLayoutConstraint(item: videoPlayerController.view, attribute: .top, relatedBy: .equal, toItem: videoView, attribute: .top, multiplier: 1, constant: 0)
let bottom = NSLayoutConstraint(item: videoPlayerController.view, attribute: .bottom, relatedBy: .equal, toItem: videoView, attribute: .bottom, multiplier: 1, constant: 0)

videoView.addConstraints([left, right, top, bottom])

let asset = AVAsset(url: url)
let clipVideoTrack = asset.tracks(withMediaType: AVMediaTypeVideo).first!
videoView.frame = CGRect(x: 0, y: 0, width: clipVideoTrack.naturalSize.height, height: clipVideoTrack.naturalSize.width)
setZoomScale()
}

fileprivate func setZoomScale() {
let widthScale = scrollView.bounds.size.width / videoView.bounds.size.width

scrollView.minimumZoomScale = widthScale
scrollView.zoomScale = widthScale
}

func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return videoView
}

func scrollViewDidZoom(_ scrollView: UIScrollView) {
let videoViewSize = videoView.frame.size
let scrollViewSize = scrollView.bounds.size

let verticalPadding = videoViewSize.height < scrollViewSize.height ? (scrollViewSize.height - videoViewSize.height) / 2 : 0
let horizontalPadding = videoViewSize.width < scrollViewSize.width ? (scrollViewSize.width - videoViewSize.width) / 2 : 0
}

scrollView.contentInset = UIEdgeInsets(top: verticalPadding, left: horizontalPadding, bottom: verticalPadding, right: horizontalPadding)
}

应该发生的是,videoView 应该填充屏幕的宽度并居中,并且能够放大到其自然大小。相反, View 在屏幕上显示为居中,但小得多(可能小 3 倍)。我做错了什么?

最佳答案

Swift5

请尝试此操作,它将为您提供所需的视频大小。

    let offsetX = max((scrollView.bounds.width - scrollView.contentSize.width) * 0.5, 0)
let offsetY = max((scrollView.bounds.height - scrollView.contentSize.height) * 0.5, 0)
scrollView.contentInset = UIEdgeInsets(top: offsetY, left: offsetX, bottom: 0, right: 0)

关于ios - 在 UIScrollView 中调整 AVPlayer 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40365347/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com