在循环中释放 renderInContext 结果
我有一个在循环中调用的方法,看起来像这样:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
自然地,经过三天的实验,我在一小时内找到了答案(无论如何,我很高兴对此发表评论)。
我添加
后
,层中的缓存内存未缓存:)。
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
after
and the cached memory in the layer is uncached :).
我没有使用 UIImageView,因此设置
layer.contents = nil
对我来说不起作用。然而,我发现了一个不同的解决方案,虽然不理想,但确实有效。看起来由renderInContext
分配的内存在 main_queue 空闲之前不会被释放。所以,我做了以下事情:这解决了我的记忆问题。我没有处理很多图像,所以我不知道性能如何受到影响。
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 byrenderInContext
does not get freed until the main_queue is idle. So, I did the following:This solved my memory issue. I'm not processing a lot of images, so I don't know how the performance is affected.