I have a method that is called in a loop that looks something like this:
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; UIImageView *background = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, PAGE_WIDTH, PAGE_HEIGHT)]; background.image = backgroundImg; for (UIView *view in viewArray) { [background addSubview:view]; } UIGraphicsBeginImageContext(background.frame.size); [background.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); for (UIView *view in background.subviews) [view removeFromSuperview]; background.image = nil; [background release]; [image retain]; [pool drain]; [image autorelease]; return image;
However, according to the Memory Memory Monitor, memory usage increases and increases and never goes down to the end of the cycle. (Failure.)
If I replaced UIGraphicsBeginImageContext with UIGraphicsEndImageContext using
UIImage * image = someotherimage;
then the memory does not burst, but is allocated and reduced at each iteration of the loop, as expected, due to the Autorelease pool. (This is not a glitch)
And if I just comment out the renderInContext line, it works fine. (Not crashing)
So it seems that renderInContext is somehow holding onto an image - how can I free it? Or any alternative suggestions please :)?
Caroline
source share