uigraphicsgetimage从当前图像上下文内存提升

问题描述

| 在我的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,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()时的内存增加有关,但不幸的是没有正确的答案,请帮忙.. ??????     

解决方法

我认为问题不在此代码中。 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 希望这能回答您的问题