2011-02-25 42 views
13

我想在PDF 創建的頁面預覽圖像,但我有一些問題,釋放內存。UIGraphicsGetImageFromCurrentImageContext內存泄漏預覽

我寫了一個簡單的測試算法的問題週期, 附近的第40次迭代應用程序崩潰:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *pdfPath = [documentsDirectory stringByAppendingPathComponent:@"myPdf.pdf"]; 
CFURLRef url = CFURLCreateWithFileSystemPath(NULL, (CFStringRef)pdfPath, kCFURLPOSIXPathStyle, NO); 
CGPDFDocumentRef myPdf = CGPDFDocumentCreateWithURL(url); 
CFRelease (url); 
CGPDFPageRef page = CGPDFDocumentGetPage(myPdf, 1); 

int i=0; 
while(i < 1000){ 

    UIGraphicsBeginImageContext(CGSizeMake(768,1024)); 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextSetRGBFillColor(context, 1.0,1.0,1.0,1.0); 
    CGContextFillRect(context,CGRectMake(0, 0, 768, 1024)); 
    CGContextSaveGState(context); 
    CGContextTranslateCTM(context, 0.0, 1024); 
    CGContextScaleCTM(context, 1.0, -1.0); 
    CGContextDrawPDFPage(context, page); 
    CGContextRestoreGState(context); 

    // -------------------------- 
    // The problem is here (without this line the application doesn't crash) 
    UIImageView *backgroundImageView1 = [[UIImageView alloc] initWithImage:UIGraphicsGetImageFromCurrentImageContext()]; 
    // -------------------------- 

    UIGraphicsEndImageContext(); 
    [backgroundImageView1 release]; 

    NSLog(@"Loop: %d", i++); 
} 

CGPDFDocumentRelease(myPdf); 

上述線似乎然而,以生成存儲器泄漏, ,儀器不顯示內存問題;

我可以從這種錯誤的逃生?有人可以解釋我以何種方式? 是否有其他方式顯示PDF預覽?

UPDATE

我覺得問題不是UIImage該方法UIGraphicsGetImageFromCurrentImageContext()UIImageView這個自動釋放的形象創造了釋放所產生的釋放。

我已分割的代碼行中的三個步驟:

UIImage *myImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIImageView *myImageView = [[UIImageView alloc] init]; 
[myImageView setImage: myImage]; // Memory Leak 

的第一和第二線不產生內存泄漏所以我認爲該方法是UIGraphicsGetImageFromCurrentImageContext不是問題。

我也嘗試如下,但問題仍然存在:

UIImageView *myImageView = [[UIImageView alloc] initWithImage:myImage]; 

我覺得這是在包含有自動釋放屬性的UIImage一個UIImageView釋放內存泄漏。

我試着寫我的對象繼承的UIImageView一個UIView如本thread解釋。

此解決方案,但不是很優雅,這是一個解決辦法,我更願意使用對象的UIImageView解決內存問題。

+0

你用什麼工具檢查內存使用情況?我會看看「分配」和「活動監視器」,它們都可以顯示泄漏工具漏掉的內存使用情況。另外,這段代碼應該做什麼呢?你想創建1000個圖像視圖,但沒有使用它們就擺脫它們? – 2011-03-21 19:38:14

+0

你找到答案了嗎?我有同樣的問題:S – 2013-12-12 23:13:40

回答

1

這段代碼在主線程上運行? UIGraphicsGetImageFromCurrentImageContext(link)的文檔說它必須以這種方式運行。

+0

是的,我閱讀了Apple文檔。我在主線程中調用了這個函數,非常感謝您的迴應! – Alessandro 2011-02-26 17:32:54

+8

從UIGraphicsGetImageFromCurrentImageContext的最新UIKit文檔:在iOS 4和更高版本中,您可以從您的應用的任何線程調用此函數。 – chown 2012-11-03 16:59:33

23

問題是這樣的:

UIGraphicsGetImageFromCurrentImageContext() 

返回一個自動釋放UIImage。 autorelease池保留這個圖像,直到你的代碼將控制返回到runloop,而你很久沒有這樣做。爲了解決這個問題,你就必須創建並耗盡你while循環在每個迭代上一個新的自動釋放池(或每幾個迭代)。

+2

我試圖在每次迭代中創建並釋放NSAutoreleasePool,但內存問題仍然存在。 非常感謝您的回覆! – Alessandro 2011-02-26 17:43:47

+0

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

+0

爲我工作。謝謝你的提示! – pstoppani 2012-03-27 18:19:35

-1

你崩潰的線,你可以像下面

得到一個UIImage的退出循環

rendered_image = UIGraphicsGetImageFromCurrentImageContext()來更新它;

9

我知道這是一個古老的問題,但我剛剛在我的頭上撞了幾個小時。在我的應用程序中反覆調用

UIImage *image = UIGraphicsGetImageFromCurrentImageContext() 

在一個循環中,儘管調用了image = nil,不知道應用程序在釋放之前會保留多久的內存,但它肯定足以讓我的應用程序獲得內存警告,然後崩潰。

我設法最終通過在@autoreleasepool中調用/使用來自UIGraphicsGetImageFromCurrentImageContext()的圖像的代碼來解決它。所以我有:

@autoreleasepool { 
    UIImage *image = [self imageWithView:_outputImageView]; //create the image 
    [movie addImage:image frameNum:i fps:kFramesPerSec]; //use the image as a frame in movie 
    image = nil; 
} 

希望可以幫助別人。

+0

這就是我的!謝謝 :) – davvilla 2014-06-04 19:50:44