在得知内存泄漏可能是导致我的iOS应用安装到手机后数小时崩溃的原因之后,我观看并阅读了有关内存泄漏的视频/文章,尤其是Xcode以及如何调试它们。我一直在尝试使用Memory Debugger和Xcode泄漏工具,但是没有运气。我想知道如何找到泄漏源?我所附的屏幕截图显示了许多泄漏之一,它从未让我深入了解图片所显示的内容。在视频中,Xcode使用回溯将它们带到引起问题的代码行,但是在任何情况下我都无法做到这一点。我还注意到泄漏来自UIKIT,这正常吗?感谢任何帮助,因为我是Xcode的新手。
这是我最初在视图控制器中登录时得到的一些代码。在第二张图片中,您将看到唯一一个存在内存泄漏的视图,但是,我也无法对此进行更深入的研究。 Second image of memory leak from InitialLogInVC
import UIKit
class IntialLoginInViewController: UIViewController {
@IBOutlet weak var backgroundAlbumArt: UIImageView!
@IBOutlet weak var foregroundAlbumArt: UIImageView!
@IBOutlet weak var musicNameLabel: UILabel!
private weak var imageOne = UIImage(named: "taste.jpg")
private weak var imageTwo = UIImage(named: "NavReckless.jpg")
private weak var imageThree = UIImage(named: "watch.jpg")
private weak var imageFour = UIImage(named: "juicewrld.jpg")
lazy var imageInformation = [(name:"Taste - Tyga", image:imageOne),(name:"Reckless - Nav", image: imageTwo),(name:"Watch - Travis Scott", image: imageThree), (name:"Goodbye & Good Riddance - Juice Wrld", image: imageFour)]
private var currentIndex = 0
static var spotifySession: AnyObject?
override func viewDidLoad() {
super.viewDidLoad()
foregroundAlbumArt.image = imageOne
backgroundAlbumArt.image = imageOne
musicNameLabel.text = imageInformation[0].name
let _ = Timer.scheduledTimer(timeInterval: 10.0, target: self, selector: #selector(imageRefresh), userInfo: nil, repeats: true)
}
override var preferredStatusBarStyle : UIStatusBarStyle {
return .lightContent
}
@objc func imageRefresh(){
if currentIndex == imageInformation.count - 1 {
currentIndex = 0
}
else {
currentIndex += 1
}
//Update Label
UIView.transition(with: self.musicNameLabel, duration: 2.0, options: [.transitionCrossDissolve] , animations: {
self.musicNameLabel.text = self.imageInformation[self.currentIndex].name
}, completion: nil)
//Update foreground Album
UIView.transition(with: self.foregroundAlbumArt, duration: 2.0, options: [.transitionCrossDissolve] , animations: {
self.foregroundAlbumArt.image = self.imageInformation[self.currentIndex].image
}, completion: nil)
//Update background Album
UIView.transition(with: self.backgroundAlbumArt, duration: 2.0, options: [.transitionCrossDissolve] , animations: {
self.backgroundAlbumArt.image = self.imageInformation[self.currentIndex].image
}, completion: nil)
}
}
答案 0 :(得分:0)
尝试
//Update Label
UIView.transition(with: self.musicNameLabel, duration: 2.0, options: [.transitionCrossDissolve] , animations: { [weak self] in
guard let `self` = self else { return }
self.musicNameLabel.text = self.imageInformation[self.currentIndex].name
}, completion: nil)
//Update foreground Album
UIView.transition(with: self.foregroundAlbumArt, duration: 2.0, options: [.transitionCrossDissolve] , animations: { [weak self] in
guard let `self` = self else { return }
self.foregroundAlbumArt.image = self.imageInformation[self.currentIndex].image
}, completion: nil)
//Update background Album
UIView.transition(with: self.backgroundAlbumArt, duration: 2.0, options: [.transitionCrossDissolve] , animations: { [weak self] in
guard let `self` = self else { return }
self.backgroundAlbumArt.image = self.imageInformation[self.currentIndex].image
}, completion: nil)