在循环中释放 renderInContext 结果

发布于 2024-10-17 03:45:45 字数 1089 浏览 0 评论 0原文

我有一个在循环中调用的方法,看起来像这样:

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
UIImageView *background = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, PAGE_WIDTH, PAGE_HEIGHT)];
background.image = backgroundImg;

for (UIView *view in viewArray)
{
    [background addSubview:view];
}

UIGraphicsBeginImageContext(background.frame.size);
[background.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

for (UIView *view in background.subviews)
    [view removeFromSuperview];

background.image = nil;
[background release];
[image retain];
[pool drain];
[image autorelease];
return image;

但是,根据 Instruments Memory Monitor,内存使用量不断上升,直到循环结束才下降。 (它崩溃了。)

如果我将 UIGraphicsBeginImageContext 替换为 UIGraphicsEndImageContext 为

UIImage *image = someotherimage;

那么内存不会激增,而是会在循环的每次迭代中分配并减少,正如我所期望的那样,由于自动释放池。 (它不会崩溃)

如果我只是注释掉 renderInContext 行,它就可以正常工作。 (不会崩溃)

所以看起来好像 renderInContext 以某种方式保留着图像 - 我怎样才能让它释放它?或者有什么替代建议吗:)?

I have a method which is called in a loop, that looks something like this:

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
UIImageView *background = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, PAGE_WIDTH, PAGE_HEIGHT)];
background.image = backgroundImg;

for (UIView *view in viewArray)
{
    [background addSubview:view];
}

UIGraphicsBeginImageContext(background.frame.size);
[background.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

for (UIView *view in background.subviews)
    [view removeFromSuperview];

background.image = nil;
[background release];
[image retain];
[pool drain];
[image autorelease];
return image;

However, according to Instruments Memory Monitor, the memory usage goes up and up, and never comes down until the end of the loop. (It crashes.)

If I replace the UIGraphicsBeginImageContext to UIGraphicsEndImageContext with

UIImage *image = someotherimage;

then the memory does not spike, but is allocated and reduces on every iteration of the loop, as I would expect, due to the Autorelease pool. (It doesn't crash)

And if I just comment out the renderInContext line it works fine. (Doesn't crash)

So it looks as if renderInContext is holding onto the image somehow - how can I get it to release it? Or any alternative suggestions please :)?

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

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

发布评论

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

评论(2

南烟 2024-10-24 03:45:45

自然地,经过三天的实验,我在一小时内找到了答案(无论如何,我很高兴对此发表评论)。

我添加

background.layer.contents = nil;

UIGraphicsEndImageContext();

,层中的缓存内存未缓存:)。

Naturally after 3 days of experimenting, I find the answer (an answer, anyway, and I would be glad of comments on this) within the hour.

I add

background.layer.contents = nil;

after

UIGraphicsEndImageContext();

and the cached memory in the layer is uncached :).

情深已缘浅 2024-10-24 03:45:45

我没有使用 UIImageView,因此设置 layer.contents = nil 对我来说不起作用。然而,我发现了一个不同的解决方案,虽然不理想,但确实有效。看起来由 renderInContext 分配的内存在 main_queue 空闲之前不会被释放。所以,我做了以下事情:

dispatch_queue_t queue = dispatch_queue_create("com.example.imageprocessing", DISPATCH_QUEUE_SERIAL);

for (int k = 0; k < images.count; ++k) {
    dispatch_async(queue, ^{
        dispatch_sync(dispatch_get_main_queue(), ^{
            @autoreleasepool {
                ...
                UIGraphicsBeginImageContext(self.view.bounds.size);
                [view.layer renderInContext:UIGraphicsGetCurrentContext()];
                image = UIGraphicsGetImageFromCurrentImageContext();
                UIGraphicsEndImageContext();
                ...
            }
        }
    }
}

这解决了我的记忆问题。我没有处理很多图像,所以我不知道性能如何受到影响。

I am not using a UIImageView, so setting layer.contents = nil did not work for me. However, I found a different solution that, while not ideal, does work. It appears that the memory allocated by renderInContext does not get freed until the main_queue is idle. So, I did the following:

dispatch_queue_t queue = dispatch_queue_create("com.example.imageprocessing", DISPATCH_QUEUE_SERIAL);

for (int k = 0; k < images.count; ++k) {
    dispatch_async(queue, ^{
        dispatch_sync(dispatch_get_main_queue(), ^{
            @autoreleasepool {
                ...
                UIGraphicsBeginImageContext(self.view.bounds.size);
                [view.layer renderInContext:UIGraphicsGetCurrentContext()];
                image = UIGraphicsGetImageFromCurrentImageContext();
                UIGraphicsEndImageContext();
                ...
            }
        }
    }
}

This solved my memory issue. I'm not processing a lot of images, so I don't know how the performance is affected.

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