[英]renderInContext consumes a lot of memory and eventually crashes
My UIView
is very large and I'm trying to convert it into a pdf. 我的
UIView
非常大,我正在尝试将其转换为pdf。 I am using renderInContext
for generating image of a UIView
. 我正在使用
renderInContext
生成UIView
图像。 After executing renderInContext
the memory goes up by 100 MB only for 1/6th of the view. 执行
renderInContext
,内存仅在视图的1/6处增加100 MB。
If the whole view is rendered, then the memory goes to around 350 MB or so in simulator but it'll crash in device. 如果呈现了整个视图,则模拟器中的内存将达到350 MB左右,但它将在设备中崩溃。 I have tried rendering view part by part, but the memory keeps increasing.
我尝试了部分渲染视图,但是内存一直在增加。
The allocated memory isn't being released immediately after creating the image. 创建映像后不会立即释放分配的内存。 I have tried changing imageNamed to imageWithContentsOfFile but no luck.
我尝试将imageNamed更改为imageWithContentsOfFile,但是没有运气。
Is there any way I can achieve this? 有什么办法可以实现?
I have referred to many answers but no luck. 我提到过很多答案,但是没有运气。
Any help would be appreciated... 任何帮助,将不胜感激...
Here is the code for generating image: 这是生成图像的代码:
UIGraphicsBeginImageContextWithOptions(aView.bounds.size, NO, 0.0);
CGContextTranslateCTM(UIGraphicsGetCurrentContext(), 0, 0);
[aView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Then I use this image to convert to pdf file. 然后,我使用此图像将其转换为pdf文件。 But while executing renderInContext I'm getting a memory spike.
但是在执行renderInContext时,我遇到了内存高峰。 It then never lowers.
然后它永远不会降低。
I've been using renderInContext recently and while it's very taxing on the CPU I saw no effect at all on memory usage. 我最近一直在使用renderInContext,尽管它在CPU上的负担非常大,但对内存使用完全没有影响。 Are you able to use the alternative:
drawViewHierarchyInRect: afterScreenUpdates:
It's much faster for me so I guess it may also be more efficient and help with your memory issue. 您是否可以使用替代方法:
drawViewHierarchyInRect: afterScreenUpdates:
对我来说,它要快得多,所以我想它可能更有效,并且可以解决您的内存问题。
That method renders to the context and then you can just call: 该方法将呈现到上下文,然后您可以调用:
CGImageRef image = CGBitmapContextCreateImage(UIGraphicsGetCurrentContext());
This answer might help someone so I'm answering it. 这个答案可能会帮助某人,所以我正在回答。
For large drawing, I had to use drawViewHierarchyInRect: afterScreenUpdates:
but it should be executed inside an autoreleasepool
. 对于大型绘图,我必须使用
drawViewHierarchyInRect: afterScreenUpdates:
但应在autoreleasepool
内部执行。 If not the memory keeps on rising. 如果不是,则内存持续增加。
For smaller drawing renderInContext
is better. 对于较小的绘图,
renderInContext
更好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.