简体   繁体   English

renderInContext占用大量内存,最终崩溃

[英]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.

相关问题 renderInContext对App Memory征税 - renderInContext taxes App Memory 今天Widget每次刷新时都会占用更多内存,最终会崩溃 - Today Widget uses more memory each time it refreshes, then eventually crashes renderInContext 和 UIImagePNGRepresentation 的内存泄漏 - Memory leak with renderInContext and UIImagePNGRepresentation iPhone X上的“ [CALayer renderInContext]”崩溃 - “[CALayer renderInContext]” crashes on iPhone X 不断增长的UIWebView导致“收到内存警告”,并最终由于“由于内存错误而终止”使应用程序崩溃 - Growing UIWebView causing “Received memory warning.” and eventually crashes app with “Terminated due to Memory Error” mergeChangesFromContextDidSaveNotification消耗内存 - mergeChangesFromContextDidSaveNotification Consumes Memory 主线程上昂贵的renderInContext内存使用情况 - Expensive renderInContext memory usage on main thread 如果未在主线程上使用,则renderincontext内存泄漏 - renderincontext Memory Leak if not use on Main thread UITextView在IOS中消耗大量内存 - UITextView consumes huge memory in IOS GrandCentralDispatch大下载队列最终耗尽内存 - GrandCentralDispatch big download queue eventually depletes memory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM