Eri*_*nce 8 xcode memory-management ios automatic-ref-counting uipageviewcontroller
我正在为基于图画书的每个基页使用单独的.h,.m和.xib文件.每个页面都加载了动画,音乐等,并占用大约4MB的内存.在Instruments中,每个页面加载时,可用内存下降约4MB.翻页时,永远不会释放此内存.它最终会给出内存警告. 似乎保持每个页面在内存中实例化,并且不会卸载它.因此,当页面快速转动时,应用程序崩溃.UIViewControllerUIPageViewControllerUIPageViewController
我希望能够卸载除了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];
Run Code Online (Sandbox Code Playgroud)
我遗漏了标准的初始化UIPageViewController.它使用" nextPageNumber"从pageArray上面拉出右页以创建新的页面对象.
-(void)turnPageForward{
[pageController setViewControllers:[NSArray arrayWithObject:[pageArray objectAtIndex:nextPageNumber]]
direction:UIPageViewControllerNavigationDirectionForward
animated:YES completion:^(BOOL finished){
}];
Run Code Online (Sandbox Code Playgroud)
}
我已经尝试创建一个对象" pageIndex"(见下文),在提供给它后将其设置为nil pageController.它没用.页面提升后,页面仍然可以很好地占用内存.
//PROGRAM PAGE FORWARD
Run Code Online (Sandbox Code Playgroud)
- (无效)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;
Run Code Online (Sandbox Code Playgroud)
}
我已经通过stackoverflow查看帖子使用相同的方式提供页面UIPageViewController,但没有发现任何接近.最接近的是" ARC在导航控制器中返回"时没有释放内存,但是没有以相同的方式设置视图控制器.
我试图将不需要的页面设置为零,因此ARC可以删除它们而没有运气.我应该尝试任何建议或替代路径?我喜欢页面卷曲效果,并且无法在其他地方找到一个好的水平页面卷发.
谢谢!埃里克
rde*_*mar 14
"UIPageViewController似乎保持每个页面在内存中实例化"
不,你是通过实例化所有那些"页面"(控制器),并将它们放入一个数组(当你翻页时内存跳转,因为控制器的视图实际上没有加载,直到你显示它,即使控制器已经实例化了.但是一旦你完成了,你的控制器就会保留它的视图并且数组会保留控制器).你只需要保留一些你所在页面的数量,并在viewControllerBeforeViewController:和viewControllerAfterViewController:的实现中,在那里实例化一个页面.当您离开该页面时,其控制器将被取消分配.如果加载速度很慢,您可能需要保留一个包含当前页面的数组以及之前和之后的数组 - 如果您将其设置为滚动而不是页面卷曲以进行转换,则UIPageViewController会执行此操作.
我做了一个像这样的简单测试应用程序:
@implementation ViewController {
int count;
}
- (void)viewDidLoad
{
[super viewDidLoad];
count = 1;
self.pager = [[UIPageViewController alloc]initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl navigationOrientation:UIPageViewControllerNavigationDirectionForward options:nil];
self.pager.dataSource = self;
self.pager.delegate = self;
Page1 *first = [[Page1 alloc] initWithNibName:@"Page1" bundle:nil];
[self.pager setViewControllers:@[first] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
[self addChildViewController:self.pager];
[self.view addSubview:self.pager.view];
[self.pager didMoveToParentViewController:self];
}
-(UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
if (count > 1) {
NSString *nibName = [@"Page" stringByAppendingFormat:@"%d",count-1];
UIViewController *prev = [[NSClassFromString(nibName) alloc] initWithNibName:nibName bundle:nil];
count -= 1;
return prev;
}
return nil;
}
-(UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
if (count < 3) {
NSString *nibName = [@"Page" stringByAppendingFormat:@"%d",count+1];
UIViewController *next = [[NSClassFromString(nibName) alloc] initWithNibName:nibName bundle:nil];
count += 1;
return next;
}
return nil;
}
Run Code Online (Sandbox Code Playgroud)
我的例子只有3页,但它说明了一种方法.我将日志放在3个控制器的dealloc方法中,当我离开它们时它们被调用.
| 归档时间: |
|
| 查看次数: |
9203 次 |
| 最近记录: |