2011-08-25 85 views
4

我寫了下面的代碼片段進行屏幕快照:UIGraphicsGetImageFromCurrentImageContext泄漏

UIGraphicsBeginImageContext(animationView.frame.size); 
[[window layer] renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage* screenshot = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

但是,UIGraphicsGetImageFromCurrentImageContext似乎被泄漏。它是否正確?

In Instruments我無法得到確切的泄漏點。在活動監視器中,我觀察到當我切換到執行上述代碼片段內存增量一些MB的UI時。在這之後,它永遠不會減少。 UIGraphicsGetImageFromCurrentImageContext是否有內存泄漏?我該如何解決這個問題?

編輯:儀器分析 活動監視器:顯示執行這行代碼時的內存加載;即使在釋放屏幕截圖(UIImage)之後也不會減少(012)。

有人可以幫助我嗎?

+0

[與預覽UIGraphicsGetImageFromCurrentImageContext內存泄漏]的可能的複製時的上下文(http://stackoverflow.com/questions/5121120/uigraphicsgetimagefromcurrentimagecontext-memory-leak-with-previews) –

回答

-1

您剛剛爲您的animationView創建了一個包含數據的UIImage(可能是一些MB)。也許你應該把這個功能包裝在一個自動釋放池中。

NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];  
    UIImage* screenshot = UIGraphicsGetImageFromCurrentImageContext(); 
    //[screenshot retain]; //If you want precise control over when it is released and you will use it later. 
    [pool release]; 
+0

我還沒有保留「屏幕截圖」。我需要釋放它嗎? – spd

+1

其實我猜UIGraphicsGetImageFromCurrentImageContext是autoreleased。嘗試將其包裝在自動釋放池中。 – cdasher

+0

我試過這個。但是,沒用。當我使用調用[[UIImage alloc] initWithContentsOfFile:]創建了一些虛擬圖像的屏幕截圖時,活動監視器釋放後真實內存減少了。 – spd

-1

CGContextRef context = UIGraphicsGetCurrentContext();

/* you code */

CGContextRelease(context);

明確做

+0

'UIGraphicsEndImageContext()'已經這樣做了。 –