uigraphicsgetimagefromcurrentimagecontext memory treike

时间:2011-04-14 09:33:00

标签: iphone ipad memory-management

在我的ipad应用程序中,我正在使用UIGraphicsGetImageFromCurrentImageContext(),内存增加非常高,应用程序崩溃了一段时间。代码如下:

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;
}

方法blendImages在循环中调用,我给了自动释放池 我在使用UIGraphicsGetImageFromCurrentImageContext()时遇到过与内存提升有关的类似问题,但遗憾的是没有正确答案,请帮忙.. ?????

1 个答案:

答案 0 :(得分:0)

我认为问题不在于此代码中。 UIGraphicsGetImageFromCurrentImageContext()返回自动释放的 UIImage *。自动释放的对象保留在自动释放池中,直到明确耗尽为止。你说你已经管理了autoreleasepool并且没有解决它。它确实为我尝试了以下代码:

-(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];
}

您还可以使用CGBitmapContextCreateImage从上下文中获取保留的CGImage。然后,您可以在完成后显式调用CGImageRelease

希望这能回答你的问题