【问题标题】:Deallocate memory used by CGContextDrawPDFPage释放 CGContextDrawPDFPage 使用的内存
【发布时间】:2011-07-10 07:03:15
【问题描述】:

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

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

【问题讨论】:

    标签: iphone cocoa-touch ios catiledlayer


    【解决方案1】:

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

    请注意,另一个常见问题是,当您发布文档时,CATiledLayer 可能会同时呈现该文档中的页面。所以,你应该充分利用@synchronize(可能使用你的pdfDocRef),只有在平铺层完成工作时才发布文档。

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

    【讨论】:

      【解决方案2】:

      我也遇到过类似的问题,

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-02-27
        • 1970-01-01
        • 2017-02-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多