释放CGContextDrawPDFPage使用的内存

6
当我使用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个回答

3

每个我认识的在iOS上处理PDF文件的人都曾经遇到过同样的问题。唯一的解决方案似乎是释放文档并重新创建。

需要注意的是,另一个常见的问题是,在您释放文档时,CATiledLayer可能同时渲染来自该文档的页面。因此,您应该充分利用@synchronize(可能使用pdfDocRef),并且仅在平铺层完成其工作后才释放文档。

还要查看这个链接:Fast and Lean PDF Viewer for iPhone / iPad / iOs - tips and hints?


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);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接