2011-03-07 98 views
4

當我使用Instruments分析我的應用程序時,我發現CGContextDrawPDFPage分配的數據不會立即釋放。由於我的程序收到很多「內存警告」,我想釋放盡可能多的內存,但我不知道如何釋放這些內存。取消分配CGContextDrawPDFPage使用的內存

正如你可以看到http://twitpic.com/473e89/full這似乎是與此相關的代碼

-(void)drawLayer:(CALayer*)layer inContext:(CGContextRef)ctx{ 
    NSAutoreleasePool * tiledViewPool = [[NSAutoreleasePool alloc] init]; 
    CGContextSetRGBFillColor(ctx, 1.0, 1.0, 1.0, 1.0); 
    CGContextFillRect(ctx, CGContextGetClipBoundingBox(ctx)); 
    CGAffineTransform pdfTransform = CGPDFPageGetDrawingTransform([self.superview.superview getPage],kCGPDFMediaBox,tiledLayer.bounds, 0, true); 
    CGContextSaveGState (ctx); 
    CGContextTranslateCTM(ctx, 0.0, tiledLayer.bounds.size.height); 
    CGContextScaleCTM(ctx, 1.0, -1.0); 
    CGContextConcatCTM (ctx, pdfTransform); 
    CGContextClipToRect (ctx, CGPDFPageGetBoxRect([self.superview.superview getPage],kCGPDFMediaBox)); 
    CGContextSetInterpolationQuality(ctx, kCGInterpolationHigh); 
    CGContextSetRenderingIntent(ctx, kCGRenderingIntentDefault); 
    CGContextDrawPDFPage(ctx,[self.superview.superview getPage]); 
    CGContextRestoreGState (ctx); 
    UIGraphicsEndPDFContext(); 
    [tiledViewPool drain]; 
} 

我已經嘗試環繞它的AutoReleasePool但是這似乎並沒有產生任何影響。在TiledView(方法所屬的視圖)被釋放之後拍攝屏幕截圖。

我希望有人能幫助我減少內存使用量。

回答

1

我也有類似的問題,

這行得到一個PDF頁面

[self.superview.superview getPage] 

確保您重新打開PDF每次拉出一個頁面時,原來CGPDFDocument做處理內存很好。

例如。像

//release the current pdf doc 
if(self.pdfDocumentRef!=nil){ 
    CGPDFDocumentRelease(self.pdfDocumentRef); 
} 
//open it again 
self.pdfDocumentRef=CGPDFDocumentCreateWithURL(..your url..); 
//pull out a page 
CGPDFPageRef pg = CGPDFDocumentGetPage(self.pdfDocumentRef, pageIndex+1); 
3

我認識的每個人在某些時候不得不在iOS上處理PDF時都遇到了同樣的問題。 唯一的解決方案似乎是發佈文檔並重新創建它。

請注意,另一個常見問題是,當您發佈文檔時,CATiledLayer可能會同時從該文檔渲染頁面。所以,你應該好好使用@synchronize(可能使用你的pdfDocRef),並且只有當平鋪層完成工作後才能釋放文檔。

另外,請查看:Fast and Lean PDF Viewer for iPhone/iPad/iOs - tips and hints?