首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >释放CGContextDrawPDFPage使用的内存

释放CGContextDrawPDFPage使用的内存
EN

Stack Overflow用户
提问于 2011-03-07 19:18:24
回答 2查看 2.6K关注 0票数 6

当我使用Instruments分析我的应用程序时,我发现CGContextDrawPDFPage分配的数据不会立即发布。由于我的程序收到了很多“内存警告”,我想释放尽可能多的内存,但我不知道如何释放这些内存。

正如您在http://twitpic.com/473e89/full上看到的,它似乎与以下代码相关

代码语言:javascript
复制
-(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 (方法所属的视图)被释放后截取的。

我希望有人能帮助我减少内存的使用。

EN

回答 2

Stack Overflow用户

发布于 2011-03-11 14:23:57

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

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

另外,请查看以下内容:Fast and Lean PDF Viewer for iPhone / iPad / iOs - tips and hints?

票数 3
EN

Stack Overflow用户

发布于 2011-03-11 14:07:36

我也遇到过类似的问题,

下面这行代码获取一个pdf页面

代码语言:javascript
复制
[self.superview.superview getPage]

确保每次你拉出一个页面时都要重新打开pdf,事实证明CGPDFDocument确实能很好地处理内存。

例如:就像这样

代码语言:javascript
复制
//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);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5219017

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档