释放 CGContextDrawPDFPage 使用的内存

发布于 2024-10-20 19:08:33 字数 1343 浏览 7 评论 0原文

当我使用 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(该方法所属的视图)被释放后拍摄的。

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

When I analyze my app with Instruments, I found out that data allocated by CGContextDrawPDFPage is not released immediately. 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

-(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. The screenshot is taken after TiledView (the view where the method belongs to) is deallocated.

I hope someone can help me to reduce memory usage.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寂寞陪衬 2024-10-27 19:08:33

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

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

另外,请查看:适用于 iPhone / iPad / iO 的快速、精益 PDF 查看器 - 提示和提示?

Everyone I know who had to deal with PDFs on iOS at some point has had this same problem.
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. 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.

Also, check this out: Fast and Lean PDF Viewer for iPhone / iPad / iOs - tips and hints?

我们的影子 2024-10-27 19:08:33

我遇到了类似的问题,

这一行获取 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);

I had a similar problem,

this line which gets a pdf page

[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.

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文