简体   繁体   中英

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

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.

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