简体   繁体   English

释放CGContextDrawPDFPage使用的内存

[英]Deallocate memory used by CGContextDrawPDFPage

When I analyze my app with Instruments, I found out that data allocated by CGContextDrawPDFPage is not released immediately. 当我用Instruments分析我的应用程序时,我发现CGContextDrawPDFPage分配的数据不会立即释放。 Since my program receives a lot of 'memory warnings' I would like to release as much memory as possible but I don't know how to release this memory. 由于我的程序收到很多“内存警告”,我想释放尽可能多的内存,但我不知道如何释放这个内存。

As you can see on http://twitpic.com/473e89/full it seems to be related to this code 正如您在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];
}

I have already tried to wrap an AutoReleasePool around it but this does not seem to have any influence. 我已经尝试在它周围包装一个AutoReleasePool,但这似乎没有任何影响。 The screenshot is taken after TiledView (the view where the method belongs to) is deallocated. 屏幕截图是在TiledView(方法所属的视图)被释放后拍摄的。

I hope someone can help me to reduce memory usage. 我希望有人可以帮我减少内存使用量。

Everyone I know who had to deal with PDFs on iOS at some point has had this same problem. 我认识的每个人在某些时候都必须处理iOS上的PDF,这也遇到了同样的问题。 The only solution seems to be to release the document and re-create it. 唯一的解决方案似乎是发布文档并重新创建它。

Note that the other common problem is that while you're releasing your document, a CATiledLayer may be rendering a page from that document at the same time. 请注意,另一个常见问题是,当您发布文档时,CATiledLayer可能会同时从该文档呈现页面。 So, you should make good use of @synchronize (probably using your pdfDocRef) and release the document only when the tiled layer has finished its work. 因此,您应该充分利用@synchronize(可能使用您的pdfDocRef)并仅在平铺图层完成其工作时释放文档。

Also, check this out: Fast and Lean PDF Viewer for iPhone / iPad / iOs - tips and hints? 另外,请查看: iPhone / iPad / iOs的快速和精益PDF查看器 - 提示和提示?

I had a similar problem, 我有类似的问题,

this line which gets a pdf page 获取pdf页面的这一行

[self.superview.superview getPage]

make sure you re-open the pdf every time you pull out a page, turns out CGPDFDocument does handle memory very well. 确保每次拉出页面时重新打开pdf,结果证明CGPDFDocument确实能很好地处理内存。

eg. 例如。 something like 就像是

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM