Search code examples
iosobjective-cipadcore-graphicsautomatic-ref-counting

How to fix terminated due to memory issue when using UIGraphicsGetImageFromCurrentImageContext?


In my drawing app I'm using UIImage to cache drawing on it.

The following line of code cause a memory issue:

UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0.0);
self.cacheImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

How can I use UIGraphicsGetImageFromCurrentImageContext without causing memory leaks ?

Any help will be appreciated. Thanks in advance.


Solution

  • Use this may be it help you. It help me in my case

     @autoreleasepool {
      // Write your code here    
     }