释放 CGContextDrawPDFPage 使用的内存
当我使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认识的每个在 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?
我遇到了类似的问题,
这一行获取 pdf 页面,
确保每次拉出页面时重新打开 pdf,事实证明 CGPDFDocument 确实可以很好地处理内存。
例如。类似的东西
I had a similar problem,
this line which gets a pdf page
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