Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retain ownership of UIImage created with UIGraphicsGetImageFromCurrentImageContext

I am using this code to get a screenshots of the window at various different times and putting the UIImage created into an array which is passed on to another UIViewController so that they can be all displayed back in a grid. I try to release the UIImage and the memory usage never goes down ... how can I use the image here once, and retain ownership so I can release the memory once it is displayed

UIGraphicsBeginImageContext(self.window.bounds.size);
[self.window.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[imagesArray addObject:image];
[image release];

like image 785
T.Leavy Avatar asked Dec 22 '22 20:12

T.Leavy


1 Answers

UIGraphicsGetImageFromCurrentImageContext() returns "an autoreleased image object containing the contents of the current bitmap graphics context."

When you add the image to imagesArray, NSMutableArray does a retain on it, and it's all you need. The memory will be released when the image is removed from the array.

You should not call release on image.

like image 90
dwery Avatar answered May 21 '23 22:05

dwery