【发布时间】:2013-01-23 05:28:16
【问题描述】:
我为基于UIPageViewController 的图画书的每个基于UIViewController 的页面使用单独的.h、.m 和.xib 文件。每个页面都加载了动画、音乐等,大约需要 4MB 的内存。在 Instruments 中,随着每一页的加载,可用内存会下降约 4MB。当页面被翻页时,这个内存永远不会被释放。它最终会给出内存警告。 UIPageViewController 似乎将它实例化的每个页面保留在内存中并且不会卸载它。因此,当页面快速翻动时,应用程序就会崩溃。
我希望能够卸载除UIPageViewController 所需的 3 个页面之外的所有页面——前一页、当前页和下一页。我如何卸载不需要的页面,因为它们是由UIPageViewController 实例化的。
下面是UIPageViewController 从中提取的页面数组。所有页面(Page1、Page2 等)基本上只是加载图像文件、提供基本动画和音乐。
//ARRAY OF PAGES
pageArray = [[NSArray alloc] initWithObjects:
(id)[[Page1 alloc] initWithNibName:nil bundle:nil],
[[Page2 alloc] initWithNibName:nil bundle:nil],
[[Page3 alloc] initWithNibName:nil bundle:nil],
[[Page4 alloc] initWithNibName:nil bundle:nil],
[[Page5 alloc] initWithNibName:nil bundle:nil],
[[Page6 alloc] initWithNibName:nil bundle:nil],
[[Page7 alloc] initWithNibName:nil bundle:nil],
[[Page8 alloc] initWithNibName:nil bundle:nil],
// continues all the way up to page 47
[[Page47 alloc] initWithNibName:nil bundle:nil],
nil];
我省略了UIPageViewController 的标准初始化。它使用“nextPageNumber”从上面的pageArray中拉出正确的页面来创建一个新的页面对象。
-(void)turnPageForward{
[pageController setViewControllers:[NSArray arrayWithObject:[pageArray objectAtIndex:nextPageNumber]]
direction:UIPageViewControllerNavigationDirectionForward
animated:YES completion:^(BOOL finished){
}];
}
我尝试创建一个对象“pageIndex”(见下文),在将其提供给pageController 后将其设置为nil。它没有用。页面前进后,页面仍然很占内存。
//PROGRAM PAGE FORWARD
-(void)turnPageForward{
UIViewController * pageIndex =[pageArray objectAtIndex:nextPageNumber]; //nextPageNumber is the next page to load
[pageController setViewControllers:[NSArray arrayWithObject:pageIndex]
direction:UIPageViewControllerNavigationDirectionForward
animated:YES completion:^(BOOL finished){
}];
pageIndex = nil;
}
我已经通过 stackoverflow 查看了使用向UIPageViewController 提供页面的相同方式的帖子,但没有找到任何接近的东西。最接近的是“ARC 在导航控制器中“返回”时没有释放内存”,但没有以相同的方式设置视图控制器。
我已尝试将不需要的页面设置为 nil,以便 ARC 可以在不走运的情况下删除它们。我应该尝试任何建议或替代路径吗?我喜欢卷页效果,但在其他地方找不到一个很好的水平卷页效果。
谢谢!埃里克
【问题讨论】:
标签: ios xcode memory-management automatic-ref-counting uipageviewcontroller