2011-03-07 102 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?