简体   繁体   中英

Resizing AVPlayer in UIScrollView

I have an AVPlayerViewController , with its AVPlayer view added as a subview of a UIView . This UIView is added to a UIScrollView . The idea is to implement a "move and scale" view for the video. I have it working for when the view inside the UIScrollView is a UIImageView . However, when the view is a UIView with an AVPlayer subview, the scale of the UIView appears too small.

Here's my code:

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)
}

What should happen is that the videoView should fill the width of the screen and be centred, with the ability to zoom in to it's natural size. Instead the view appears centred but much smaller (maybe 3 times smaller) on the screen. What am I doing wrong?

Swift5

please try this, it will give you the video size you need.

    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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM