如何解决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
希望这能回答您的问题
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。