ViewController deinit 后 UIImageView 内存泄漏

Memory leak on UIImageView after ViewController deinit

我有一个 ViewController,其中包含填充整个 vc 的 imageView 的子视图,当我呈现新的 VC 并关闭旧的 VC 时,deinit of oldVC 被调用但内存仍然存在并且仪器显示我是与 imageio 相关的东西,主要问题是为什么旧 vc 的 deinit 被调用但内存中的图像仍然存在..这是我的旧vc代码:

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")
}
}

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

感谢 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.