【发布时间】:2020-07-26 21:20:20
【问题描述】:
这是我为我构建的第一个应用程序修复内存泄漏的第一次尝试。当我使用 Leaks 工具时,我的登录视图控制器上出现 1 个泄漏,据我所知,这与我用于循环视频背景的 AudioToolBox (AVFoundation) 有关。
所以我想我会尝试将AVPlayer 变量设置为weak,但这对泄漏没有任何作用。
如何正确修复此泄漏?
这是与循环背景视频相关的代码...
weak var player : AVPlayer?
weak var playerLayer : AVPlayerLayer?
override func viewDidLoad() {
super.viewDidLoad()
playBackgroundVideo()
}
func playBackgroundVideo() {
guard let path = Bundle.main.path(forResource: "dumbbells2480", ofType: "mov") else { return }
player = AVPlayer(url: URL(fileURLWithPath: path))
player!.actionAtItemEnd = AVPlayer.ActionAtItemEnd.none
playerLayer = AVPlayerLayer(player: player)
playerLayer!.frame = self.view.frame
playerLayer!.videoGravity = AVLayerVideoGravity.resizeAspectFill
self.view.layer.insertSublayer(playerLayer!, at: 0)
NotificationCenter.default.addObserver(self, selector: #selector(playerItemDidReachEnd), name: Notification.Name.AVPlayerItemDidPlayToEndTime, object: player!.currentItem)
NotificationCenter.default.addObserver(self,selector: #selector(playItem),name: UIApplication.willEnterForegroundNotification, object: nil)
player!.seek(to: CMTime.zero)
player!.play()
self.player?.isMuted = true
view.bringSubviewToFront(authStackView)
}
@objc func playerItemDidReachEnd(){
player!.seek(to: CMTime.zero)
}
@objc private func playItem() {
playerItemDidReachEnd()
player?.play()
if let playerlayer = playerLayer {
view.layer.insertSublayer(playerlayer, at: 0)
}
}
【问题讨论】:
-
一次泄露了 16 个字节,您担心吗?
-
????♂️对不起,我真的不知道我应该或不应该担心什么。我的印象是几乎任何泄漏都需要解决,但如果我错了,请纠正我。我是个新手。
标签: ios swift memory-leaks