释放CGContextDrawPDFPage使用的内存

时间:2011-03-07 11:18:24

标签: iphone cocoa-touch ios catiledlayer

当我使用Instruments分析我的应用程序时,我发现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(方法所属的视图)被解除分配后拍摄的。

我希望有人可以帮我减少内存使用量。

2 个答案:

答案 0 :(得分:3)

我认识的每个人在某些时候都必须处理iOS上的PDF,这也存在同样的问题。 唯一的解决方案似乎是发布文档并重新创建它。

请注意,另一个常见问题是,当您发布文档时,CATiledLayer可能会同时呈现该文档中的页面。因此,您应该充分利用@synchronize(可能使用您的pdfDocRef)并仅在平铺图层完成其工作时释放文档。

另外,请检查一下:Fast and Lean PDF Viewer for iPhone / iPad / iOs - tips and hints?

答案 1 :(得分: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);