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

查看:16
本文介绍了在 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 运行了该应用程序.每次翻页,应用程序的Real Memory消耗增加约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).

  • 为什么这么大的内存消耗?这是因为 Storyboard 场景中的 UIImageView 缓存了图像吗?

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

使用UINavigationController 和 ARC?

我尝试在视图控制器的 viewDidDisappear: 方法中将所有视图控制器的子视图设置为 nil,但内存消耗保持不变.

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 上的后退按钮.它保留了一堆 ViewController - 可见的在顶部.

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天全站免登陆