2011-04-14 65 views
2

在我使用UIGraphicsGetImageFromCurrentImageContext()的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()時,與記憶加息,但遺憾的是沒有正確答案,任何幫助,請.. ..

+0

你有沒有得到任何解決方案? – spd 2011-08-26 12:01:50

+0

我有同樣的問題,也許如果我們將UIImage更改爲另一種格式表示形式? – 2011-10-18 20:47:40

+0

[UIGraphicsGetImageFromCurrentImageContext內存泄漏與預覽]的可能重複(http://stackoverflow.com/questions/5121120/uigraphicsgetimagefromcurrentimagecontext-memory-leak-with-previews) – 2016-12-24 05:09:42

回答

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當你完成

希望這可以回答你的問題

相關問題