iosswiftavplayeravplayerlayer

Swift AVPlayerLayer videoGravity .resizeAspect only respects height not width


I have an AVPlayer and AVPlayerLayer embedded in a simple UIView. This plays the movie but adjusting playerLayer.videoGravity only effects the movies relative height:

  1. .resizeAspect fills all the way to side of the videoview UIView and crops the height
  2. .resizeAspectFill looks like a zoomed in version of the stretched .resizeAspect
    var player = AVPlayer()
    @IBOutlet weak var videoview: UIView!
    override func viewDidAppear(_ animated: Bool) {

        player = AVPlayer(url: url)
        let playerLayer = AVPlayerLayer(player: player)
                
        playerLayer.setAffineTransform(CGAffineTransform(rotationAngle: CGFloat((-90 * Double.pi)/180)))
                                       
        playerLayer.frame = videoview.bounds
        playerLayer.videoGravity = .resizeAspect

        videoview.layer.addSublayer(playerLayer)     
        
        player.play()
    }


Solution

  • The issue here was with playerLayer.setAffineTransform(CGAffineTransform(rotationAngle: CGFloat((-90 * Double.pi)/180))) and the video/image size. The wrong height/width were used previously (and needed to be transposed). This resolved the issue.