2

In my ipad application i am using UIGraphicsGetImageFromCurrentImageContext(), the memory increases very high and the app crashes sometime. Code is given below

UIImage * blendImages(UIImage *background, UIImage *overlay)
{
    UIImageView* imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, 1024.0,700.0)];
    UIImageView* subView   = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, 1024.0,700.0)];
    subView.alpha = 1.0; 
    [imageView addSubview:subView];
    imageView.image=background;
    subView.image=overlay;
    UIGraphicsBeginImageContext(imageView.frame.size);
    [imageView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage* blendedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    [subView release];
    [imageView release];    
    return blendedImage;
}

The method blendImages is called with in a loop and i had given autorelease pool I have seen similar questions asked , related to memory hike when using UIGraphicsGetImageFromCurrentImageContext() , but unfortunately no proper answer , Any help please ..?????

lee
  • 21
  • 3
  • I have the same problem, maybe if we changed the UIImage to another format representation? – the Reverend Oct 18 '11 at 20:47
  • Possible duplicate of [UIGraphicsGetImageFromCurrentImageContext memory leak with previews](http://stackoverflow.com/questions/5121120/uigraphicsgetimagefromcurrentimagecontext-memory-leak-with-previews) – Cœur Dec 24 '16 at 05:09

1 Answers1

0

I don't think the problem is in this code. UIGraphicsGetImageFromCurrentImageContext() returns an autoreleased UIImage*. Autoreleased objects remain in the autorelease pool until it is explicitly drained. You say you have managed the autoreleasepool and that didn't solve it. It did for me try the following code:

-(void)someMethod{
//interesting stuff

//start a new inner pool 
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
UIImage* image = [blendImages(backGround, overlay)retain];

//do things with image

//first release our strong reference to the object
[image release];
//drain the inner pool from all autoreleased objects
[pool release];
}

You can also use CGBitmapContextCreateImage to get a retained CGImage from the context. You can then explicitly call CGImageRelease when you're done

hope this answers your question