Воспроизведение видео внутри UIView обрезки до границ просмотра - PullRequest
0 голосов
/ 11 июля 2020
• 1000

Вот код:

class PlaceHolderVideoView : UIView{

let containerImageView = UIImageView(image: #imageLiteral(resourceName: "VideoContainerView"), contentMode: .scaleAspectFit)



override init(frame: CGRect) {
    super.init(frame: frame)
    
    setUpUI()
    
    setUpPlayer()
    
    
    
}

fileprivate func setUpUI(){
    
    translatesAutoresizingMaskIntoConstraints = false
    
    addSubview(containerImageView)
    containerImageView.fillSuperview()
    
}



fileprivate func setUpPlayer(){

    let urlPathString = Bundle.main.path(forResource: "dance", ofType: "mp4")

    if let videoURL = urlPathString{
        let url = URL(fileURLWithPath: videoURL)

        let player = AVPlayer(url: url)
        let playerLayer = AVPlayerLayer(player: player)


        playerLayer.frame = self.containerImageView.frame

        self.layer.addSublayer(playerLayer)

        player.play()
    }
}

1 Ответ

0 голосов
/ 11 июля 2020

Используйте clipsToBounds.

class PlaceHolderVideoView: UIView {
    //...
    override init(frame: CGRect) {
        super.init(frame: frame)
        clipsToBounds = true
        //...
    }
    //...
    fileprivate func setUpPlayer() {
        //..
        playerLayer.frame = containerImageView.bounds
    }
}
...