在UINavigationController上推送许多视图控制器时出现内存问题 [英] Memory problems when pushing many view controllers on UINavigationController

查看:106
本文介绍了在UINavigationController上推送许多视图控制器时出现内存问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为iPad制作一本交互式书籍,并使用 UINavigationController 来实现页面与下一页之间的导航。当用户翻页时,下一页被推到导航堆栈的顶部。

I'm making an interactive book for the iPad and am using UINavigationController to implement the navigation between a page and the next. When a user turns the page, the next page is pushed on top of the navigation stack.

我现在15页进入应用程序,当我尝试从第14页转到第15页时应用程序崩溃。控制台中没有错误消息,没有任何内容设备的崩溃记录也没有。

I'm now 15 pages into the app, and the app crashes when I try to go from page 14 to page 15. No error message in the console, nothing in the device's crash logs neither.

故事板中的每个视图控制器场景都有 UIImageViews ,显示5MB到20MB之间的图像。视图控制器的 viewDidLoad 方法只调用一次。所有应用程序资产的总大小低于200B。我正在使用ARC。

Each view controller's scene in the storyboard has UIImageViews displaying images that are between 5MB and 20MB. The view controllers' viewDidLoad method is called just once. The total size of all the app's assets is below 200B. I'm using ARC.

我使用Instruments的Memory Monitor运行了应用程序。每次翻页时应用程序的实内存消耗量增加约80MB,当达到800MB时该应用程序崩溃(该设备是iPad 3)。

I've ran the app using Instruments' Memory Monitor. The app's Real Memory consumption increases by about 80MB every time a new page is turned, and crashes when it reaches 800MB (the device is an iPad 3).


  • 为什么这么大的内存消耗?这是因为故事板场景中的UIImageView缓存了图片?

  • Why such an enormous memory consumption? Is this because the UIImageView in the Storyboard's scenes cache the images?

当你使用
UINavigationController 时,释放内存的最佳方法是什么?和ARC?

What would be the best way to free up memory when you use a UINavigationController and ARC?

我尝试在视图控制器'<$ c $中将所有视图控制器的子视图添加到nil c> viewDidDisappear:方法,但内存消耗保持不变。

I tried adding setting all the view controller's subviews to nil in the view controllers' viewDidDisappear: method, but memory consumption stayed the same.

推荐答案

当你使用UINavigationController时,你推送的每个ViewController永远都会留在内存中(好吧,直到你的应用程序退出),除非你的用户按下该特定ViewController上的后退按钮。它保留了一堆ViewControllers - 顶部有一个可见的。

When you use a UINavigationController, each ViewController you push stays in memory forever (well, until your app exits) unless your user presses the back button on that particular ViewController. It keeps a stack of ViewControllers - with the visible one at the top.

所以简单的答案是不要使用UINavigationController这个
我建议建立你自己的ViewController,它'知道'哪个是下一页和前一页,并在需要时手动加载和删除它们。通过这种方式,您可以确保一次只在内存中有一个页面(在转换期间除外 - 也许您可以将此动画用于转换 http://cocoacontrols.com/platforms/ios/controls/xbpagecurl )。

So the simple answer is don't use a UINavigationController for this. I'd suggest building your own ViewController that 'knows' which is the next and previous pages and manually loads and removes them as and when required. This way you can ensure that you only have one page in memory at once (except for during the transitions - maybe you could use this animation for the transitions http://cocoacontrols.com/platforms/ios/controls/xbpagecurl).

这篇关于在UINavigationController上推送许多视图控制器时出现内存问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆