Swift iOS:使用以下代码泄漏内存(video播放器)

我用游戏中的永久循环播放全屏video(用于背景)。 它工作的很好,但是当我改变视图时,我得到这个代码上的Instruments(内存泄漏模板)标识的内存泄漏:

urlStr = NSBundle.mainBundle().pathForResource("Video_Socle", ofType: "mov") let url = NSURL(fileURLWithPath: urlStr!) player = AVPlayer(URL: url) NSNotificationCenter.defaultCenter().addObserverForName(AVPlayerItemDidPlayToEndTimeNotification, object: player!.currentItem, queue: nil) { notification in let t1 = CMTimeMake(5, 100); self.player!.seekToTime(t1) self.player!.play() } videoNode = SKVideoNode(AVPlayer: player!) videoNode!.anchorPoint = CGPointMake (0,0) videoNode!.size = CGSize(width: 2048, height: 1536) videoNode!.zPosition = 0 background.addChild(videoNode!) if synch == false { video_synchronization() } videoNode!.play() 

我用下面的代码创build了一个小的清理函数:

 NSNotificationCenter.defaultCenter().removeObserver(self) player = nil videoNode = nil background.removeAllActions() background.removeAllChildren() let transition = SKTransition.revealWithDirection(.Right, duration: 2) let nextScene = MainView(size: scene!.size) nextScene.scaleMode = .AspectFill scene?.view?.presentScene(nextScene, transition: transition) self.viewController?.dismissViewControllerAnimated(true, completion: nil) 

我得到AVPlayer和SKVideoNode上识别的内存泄漏。 奇怪的是,当我删除零件泄漏消失:

 NSNotificationCenter.defaultCenter().addObserverForName(AVPlayerItemDidPlayToEndTimeNotification, object: player!.currentItem, queue: nil) { notification in let t1 = CMTimeMake(5, 100); self.player!.seekToTime(t1) self.player!.play() } 

这个代码我错过了什么? 谢谢 !

当您更改视图时,您需要执行以下操作。

如果你有什么观点,你正在播放video..你需要删除它。

view.removeFromSuperView()