Krisiacik / ImageViewer

An image viewer à la Twitter
MIT License
2.53k stars 385 forks source link

Video Cropping Issue #211

Closed alimirshad closed 5 years ago

alimirshad commented 5 years ago

Hello,

I am using this pod in my app. The video is square and getting cropped to 16:9. Because of this the video is getting cropped from the top. How to solve this.

In my app

img_4749

ORIGINAL

img_5aed2aad8b0e-1

alimirshad commented 5 years ago

Hello @RuiAAPeres

Can you help me on this.

alimirshad commented 5 years ago

Fixed..

ItemBaseController.swift

`override open func viewDidLayoutSubviews() {

    super.viewDidLayoutSubviews()

    scrollView.frame = self.view.bounds
    activityIndicatorView.center = view.boundsCenter

    //this is added
    if let itemView = itemView as? VideoView{

        itemView.frame = self.view.frame

        itemView.center = scrollView.boundsCenter

    }
    else{
        if let size = itemView.image?.size , size != CGSize.zero {

            let aspectFitItemSize = aspectFitSize(forContentOfSize: size, inBounds: self.scrollView.bounds.size)

            itemView.bounds.size = aspectFitItemSize
            scrollView.contentSize = itemView.bounds.size

            itemView.center = scrollView.boundsCenter

        }
    }
    //end here

// if let size = itemView.image?.size , size != CGSize.zero { //
// let aspectFitItemSize = aspectFitSize(forContentOfSize: size, inBounds: self.scrollView.bounds.size) //
// itemView.bounds.size = aspectFitItemSize // scrollView.contentSize = itemView.bounds.size //
// itemView.center = scrollView.boundsCenter // } }`

VideoView.swift

`var player: AVPlayer? {

    willSet {

        if newValue == nil {

            player?.removeObserver(self, forKeyPath: "status")
            player?.removeObserver(self, forKeyPath: "rate")
        }
    }

    didSet {

        if  let player = self.player,
            let videoLayer = self.layer as? AVPlayerLayer {

            videoLayer.player = player
            //videoLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill
            videoLayer.videoGravity = AVLayerVideoGravity.resizeAspect

            player.addObserver(self, forKeyPath: "status", options: NSKeyValueObservingOptions.new, context: nil)
            player.addObserver(self, forKeyPath: "rate", options: NSKeyValueObservingOptions.new, context: nil)
        }
    }
}`
mateusz-as commented 5 years ago

You should also change to previewImageView.contentMode = .scaleAspectFit in VideoView.swift init. Because then you can properly display vertical and horizontal video preview images.