uigraphicsgetimagefromcurrentimagecontext memory hike
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 t开发者_StackOverflow社区o memory hike when using UIGraphicsGetImageFromCurrentImageContext() , but unfortunately no proper answer , Any help please ..?????
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
精彩评论