ios – 在UIPageViewController中释放未使用的页面

我正在为基于UIPageViewController的图画书的每个基于UIViewController的页面使用单独的.h,.m和.xib文件.每个页面都加载了动画,音乐等,并占用大约4MB的内存.在Instruments中,每个页面加载时,可用内存下降约4MB.翻页时,永远不会释放此内存.它最终会给出内存警告. UIPageViewController似乎保持每个页面在内存中实例化,并且不会卸载它.因此,当页面快速转动时,应用程序崩溃.

我希望能够卸载UIPageViewController所需的所有页面 – 前一页,当前页和下一页.我怎样才能卸载不需要的页面,因为它们是由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

– (无效)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在导航控制器中返回”时没有释放内存,但是没有以相同的方式设置视图控制器.

我试图将不需要的页面设置为零,因此ARC可以删除它们而没有运气.我应该尝试任何建议或替代路径?我喜欢页面卷曲效果,并且无法在其他地方找到一个好的水平页面卷发.

谢谢!埃里克

解决方法

“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;
}

我的例子只有3页,但它说明了一种方法.我将日志放在3个控制器的dealloc方法中,当我离开它们时它们被调用.

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...