如何删除我添加播放AVplayer Swift的图层

时间:2016-02-12 13:58:31

标签: swift avplayer

我有一个应用程序重新进入相​​同的视图,每次为mp4视频创建一个图层,创建一个内存蔓延,因为图层没有被删除,或者至少这是我的猜测。我怎样才能删除添加的图层?

override func viewWillAppear(animated: Bool) {



     let videoURL: NSURL = NSBundle.mainBundle().URLForResource("info2", withExtension: "mp4")!


        mygtukas = AVPlayer(URL: videoURL)
        mygtukas?.actionAtItemEnd = .None
        mygtukas?.muted = true



        let playerLayer = AVPlayerLayer(player: mygtukas)
        playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
        playerLayer.zPosition = 1
        playerLayer.frame = CGRect(x:20.0, y: 703.0, width: 36.0, height: 36.0)


        view.layer.addSublayer(playerLayer)

        mygtukas?.play()

        NSNotificationCenter.defaultCenter().addObserver(self,
            selector: "loopVideo",
            name: AVPlayerItemDidPlayToEndTimeNotification,
            object:nil)




}

1 个答案:

答案 0 :(得分:1)

这应该有效:

将playerLayer设为类实例并尝试以下操作。 在loopVideo方法中调用:

  

playerLayer.removeFromSuperlayer()