gpt4 book ai didi

ios - ViewController deinit 后 UIImageView 内存泄漏

转载 作者:行者123 更新时间:2023-11-28 06:02:37 24 4
gpt4 key购买 nike

我有一个 ViewController,带有填充整个 vc 的 imageView 的 subview ,当我呈现新的 VC 并关闭旧的 VC 时,调用了 oldVC 的 deinit 但内存仍然存在,仪器显示我与 imageio 相关,主要问题是为什么调用 oldvc 的 deinit 但图像在内存中保留..这是我的 oldvc 代码:

class Vc1: UIViewController {

@IBOutlet weak var vcimage: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
vcimage.image = UIImage.init(named: "splash")
// Do any additional setup after loading the view.
}
@IBAction func tovc2(_ sender: Any) {
let vc = self.storyboard?.instantiateViewController(withIdentifier: "vc2")
AnimateTovc(ViewControllerToAnimate: vc!, vctoDissmiss: self)
}
override func didReceiveMemoryWarning() {
print("memory warning")
}
func AnimateTovc(duration:Float = 0.5,Animateoption:UIViewAnimationOptions = .transitionFlipFromLeft,ViewControllerToAnimate vc:UIViewController,vctoDissmiss: UIViewController?,completion : @escaping ()->Void = {} ){
let appDelegate = UIApplication.shared.delegate as! AppDelegate
UIView.transition(with: appDelegate.window!, duration: TimeInterval(duration), options: Animateoption , animations: { () -> Void in
appDelegate.window!.rootViewController = vc
}, completion:{ isfinished in
completion()
vctoDissmiss?.dismiss(animated: false, completion: nil)
})
}
deinit {
print("removed \(self) from memory")
}
}

奇怪的是,当应用程序进入后台时,内存在 newvc 中被释放,而 imageio 部分从内存中释放。

最佳答案

感谢 Allen,使用 UIImage(contentsOfFile:) 解决了这个问题。

根据 Apple 文档:

Use the init(named:in:compatibleWith:) method (or the init(named:) method) to create an image from an image asset or image file located in your app’s main bundle (or some other known bundle). Because these methods cache the image data automatically, they are especially recommended for images that you use frequently.

Use the imageWithContentsOfFile: or init(contentsOfFile:) method to create an image object where the initial data is not in a bundle. These methods load the image data from disk each time, so you should not use them to load the same image repeatedly.

关于ios - ViewController deinit 后 UIImageView 内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49202575/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com