【发布时间】:2018-08-18 13:05:40
【问题描述】:
我有一个 ViewController,它带有填充整个 vc 的 imageView 的子视图,当我展示新 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 部分从内存中释放。
【问题讨论】:
-
请注意,模拟器存在内存泄漏,这是由于模拟器中的错误造成的,但实际设备上不会发生这种情况。如果您遇到没有意义的内存泄漏,请务必在设备上进行测试并查看泄漏仍然发生。
-
@DuncanC 不幸的是,真实设备上也不存在问题。
-
尝试使用
UIImage(contentsOfFile: pathForImage)创建图像 -
@AllenR 是的!使用这种方法可以释放内存..所以每当我有高质量的图像或想要使用一次图像时,我都会使用这种方法?
-
是的,我真的希望有更好的 api。确实应该更明显的是,方法签名正在缓存图像。当您需要一遍又一遍地使用相同的图像时,请谨慎使用它。
标签: ios swift memory-leaks uiimageview