IOS:切换 View 时内存占用增加(启用ARC)

标签 ios view automatic-ref-counting xib

我是 X-code 的新手,一直在到处搜索并尝试“一切”,但没有成功。 如果你能帮助我,我会给你唱首歌:)

我正在制作一个占用大量内存的 iPad 应用程序。该项目的大小约为 100mb(许多图像),但事件监视器显示我在所有 View (xib 文件)之间切换后使用了高达 140mb 的“实际内存”:(

每次在 View (xib 文件)之间切换时,我的内存占用都会增加。 ARC 已启用,因此我无法从内存中释放之前的 View 。我有 10 个页面(xib 文件)可以切换。

我使用这段代码在 xib 文件之间切换:

viewController2 *view2 = [[viewController2 alloc] initWithNibName:@"viewController2" bundle:nil];
view2.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:view2 animated:YES];

当我切换回或切换到下一个 View 时,我使用相同类型的代码。

我试过使用 [self dismissModalViewControllerAnimated:NO];, [self.view removeFromSuperview];, self.view = nil; 但是内存不断增加。

据我了解, View 相互堆叠,导致内存增加。

如何切换到下一个 View (xib 文件)并同时终止第一个 View 的内存使用?

这个问题让我费尽了心思:(

最佳答案

我有一个包含 40 个不同的全屏 View Controller 的应用程序。这是一种交互式幻灯片放映,其中每张幻灯片都有某种互动。我用一个主视图 Controller 设置了这个应用程序,它控制在哪个时间显示哪个单独的 View Controller 。我从一个 View Controller 过渡到下一个 View Controller ,方法是将新 View Controller 添加为 subview Controller ,并将新 View Controller 的 View 添加为主视图 Controller View 的 subview :

[self addChildViewController:newView];
[self.view addSubview:newView.view];

self 是主视图 Controller 。然后我使用 UIViewtransitionWithView:duration:options:animations:completion: 类方法来显示新 View 。在完成 block 中, View 和 View Controller 被删除,允许 ARC 处理该内存(只要没有其他对这些对象的强引用):

void (^theCompletion)(BOOL) = ^(BOOL finished) { 
    [oldView.view removeFromSuperview]; 
    [oldView removeFromParentViewController]; 
};

关于IOS:切换 View 时内存占用增加(启用ARC),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10437473/

相关文章:

IOS自动续订订阅状态获取不到

android - ?selectableItemBackgroundBorderless 不工作

swift - 为什么Operation Queue中不需要[weak self]或[unowned self]?

iOS:将结构作为参数传递

iphone - Apple 因恢复 IAP 而拒绝了我的 App

android - 序列化 Android View - 从 IllegalAccessException 导致 InvalidClassException

ios - __strong 非 ARC 项目中使用的限定符

ios - ARC ios 中的内存压力

ios - Appcode找不到目的地

drupal - 使用多个选项按字段对 Drupal View 进行分组 - 仅显示一个字段