Reputation: 254
I have a ViewController with subview of imageView of filling whole of vc , when I present new VC and dismiss the old VC , deinit of oldVC is called but memory remained and instrument shows me is something related to imageio , the main problem is why deinit of oldvc is called but image reamins on memory.. this is my oldvc code:
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")
}
}
the weird part is memory gets freed in newvc when app gets in background and imageio part is release from memory.
Upvotes: 2
Views: 1258
Reputation: 254
Thanks to Allen, the problem is solved by using UIImage(contentsOfFile:)
.
According to Apple docs:
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.
Upvotes: 3