UINavigationController:弹出时释放ViewController的内存

发布于 2024-12-11 01:05:22 字数 727 浏览 0 评论 0原文

我在这里看到了一些关于这个主题的帖子,但没有明确的答案。这是我的问题。

我有一个UINavigationController,我将其用作画廊。在第一个控制器上,我加载了一堆远程图像。这会增加我的内存大小,但不会增加那么多。单击图像时,它将推送到另一个 viewController 上,其中包含刚刚单击的图库的图像。这可能会从这些图像中加载另外 1MB 或更多的数据。

这里的问题是用户可能会浏览任意数量的这些画廊。因为当我弹出 viewController 时,内存没有被释放,当用户继续浏览图库时,我的应用程序中开始使用过多的内存。

当我弹出 viewController 时,有什么方法可以释放这些内存吗?也许在我的 viewDidDisappear: 方法中?如果是这样,我会释放什么?我怎样才能再次创建它?我尝试过这一点,例如发布我的观点,但我遇到了崩溃。

对这个问题有什么见解吗?

PhotosGalleryiPad *gallery = [[PhotosGalleryiPad alloc] init];
gallery.items = self.items;
gallery.asset = self.currentAsset;
[self.navigationController pushViewController:gallery animated:YES];
[gallery release];

I have seen a few posts on this subject here on SO, but no definitive answers. Here is my problem.

I have a UINavigationController which I use as a gallery. On the first controller I load up a bunch of remote images. This increases my memory size, but not by that much. When clicking an image, it will push on another viewController which has images for the gallery just clicked. This might load in another 1MB or more of data from those images.

The problem here is that a user might browse any number of these galleries. Since when I pop the viewController, that memory is not released I start to get too much memory usage in my app when the users continues to browse the galleries.

Is there any way that I can release this memory when I pop my viewController? Perhaps in my viewDidDisappear: method? If so, what would I release? And how can I create it again? I tried this to a point, such as releasing my view, but I get crashes.

Any insight into this issue?

PhotosGalleryiPad *gallery = [[PhotosGalleryiPad alloc] init];
gallery.items = self.items;
gallery.asset = self.currentAsset;
[self.navigationController pushViewController:gallery animated:YES];
[gallery release];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

难以启齿的温柔 2024-12-18 01:05:22

通常,您可以在视图控制器的 dealloc 方法中释放从堆栈中弹出的所有内存。

但是,如果您正在谈论图像并使用 [UIImage imageNamed:] 加载它们,那么 UIKit 可能会缓存它们。尝试在模拟器中伪造内存警告(从硬件菜单),看看是否会卸载这些缓存的图像。

您还可以在 Instruments 中进行堆快照分析。在加载视图控制器之前标记堆,在关闭视图控制器后再次标记它,并查看哪些对象保留在周围。

You normally release any memory in the dealloc method of the view controller that gets popped off the stack.

However, if you're talking about images and you loaded them with [UIImage imageNamed:] then UIKit may be caching them. Try faking a memory warning in the simulator (from the Hardware menu) and see if that unloads these cached images.

You can also do a heapshot analysis in Instruments. Mark the heap before loading the view controller, mark it again after closing the view controller, and see which objects stick around.

关于从前 2024-12-18 01:05:22

您到底在哪里保留这些画廊图像?

让我猜测 gallery.items 是一个保存图像的可变集合(可能是一个数组)。当用户访问新的画廊时,更多图像会添加到该数组中。从视图控制器到视图控制器,您将指针传递给该数组:

gallery.items = self.items;

因此,当您弹出视图控制器时,您仍然保留相同的放大数组。那么,问题是当您弹出视图控制器时如何剔除新添加的图像数组。

您可以创建一个 集合的浅拷贝。如果它是一个可变数组,您可以执行以下操作:

gallery.items = [self.items mutableCopyWithZone:nil];

然后,当弹出视图控制器时,它的 items 数组被释放。弹出的视图控制器添加的对象被释放,但旧的对象仍然保留在前一个视图控制器的 items 数组中。

(我只是猜测。如果我错了,如果您解释一下保留这些图像的位置将会很有帮助。)

Where, exactly, are you retaining these gallery images?

Let me take a guess that gallery.items is a mutable collection (possibly an array) holding the images. As the user visits new galleries, more images are added to this array. From view controller to view controller, you are passing a pointer to this array:

gallery.items = self.items;

So, when you pop the view controller, you are still left with the same, enlarged, array. The issue, then, is how to cull this array of the newly added images when you pop a view controller.

Rather than passing a pointer, you could make a shallow copy of the collection. If it's a mutable array, you could do as follows:

gallery.items = [self.items mutableCopyWithZone:nil];

Then, when a view controller is popped, its items array is released. The objects added by the popped view controller are released, but the old objects are still retained in the previous view controller's items array.

(I'm just taking a guess. If I'm wrong, it would be helpful if you explained where you're retaining these images.)

祁梦 2024-12-18 01:05:22

如果您像这样将视图“弹出”到前台:

infoScreen = [[[infoScreen alloc] initWithNibName:@"InfoScreen" bundle:nil] autorelease];
infoScreen.view.center = CGPointMake(self.view.bounds.size.width / 2, self.view.bounds.size.height / 2);
[self.view sendSubviewToBack:self.view];

那么您将执行 [infoScreen release];
从内存中释放视图。
记住您分配保留的任何内容(还有其他情况,但我马上就忘记了)您需要释放

If you are "popping" views onto the foreground like this:

infoScreen = [[[infoScreen alloc] initWithNibName:@"InfoScreen" bundle:nil] autorelease];
infoScreen.view.center = CGPointMake(self.view.bounds.size.width / 2, self.view.bounds.size.height / 2);
[self.view sendSubviewToBack:self.view];

Then you would do [infoScreen release];
to release/free the view from memory.
Remember anything you alloc, or retain (there are other situations too but i forget them off hand) you need to release.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文