Error UIGraphicsGetImageFromCurrentImageContext

I wrote the following code snippet to take a screenshot:

UIGraphicsBeginImageContext(animationView.frame.size); [[window layer] renderInContext:UIGraphicsGetCurrentContext()]; UIImage* screenshot = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); 

But, UIGraphicsGetImageFromCurrentImageContext seems to be leaking. Is it correct?

InstrumentsI was unable to obtain the exact leak point. In the activity monitor, I noticed that when I switch to a user interface that executes the code snippets described above, the code snippet is on some MB. After this moment, it never decreases. Does UIGraphicsGetImageFromCurrentImageContext have a memory leak? How to solve this?

Edit: analysis of tools Activity Monitor: shows a burst of memory when this line of code is executed; never decreases even after taking a screenshot (UIImage)

Leaks and distribution, heap Snapshot: does not show leaks or this distribution.

Can someone help me?

+4
source share
2 answers

You just created a UIImage with data for your animation screen (which may be some MB). Perhaps you should transfer this functionality to the autorun pool.

  NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; UIImage* screenshot = UIGraphicsGetImageFromCurrentImageContext(); //[screenshot retain]; //If you want precise control over when it is released and you will use it later. [pool release]; 
-1
source

CGContextRef context = UIGraphicsGetCurrenttext ();

/* code */

CGContextRelease (context);

clear context on completion

-1
source

Source: https://habr.com/ru/post/1368959/


All Articles