iphone - 释放 CGContextDrawPDFPage 使用的内存

标签 iphone cocoa-touch ios catiledlayer

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

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

最佳答案

我认识的每个人在某个时候都必须在 iOS 上处理 PDF 时遇到过同样的问题。 唯一的解决办法似乎是发布文档并重新创建它。

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

另外,检查一下:Fast and Lean PDF Viewer for iPhone / iPad / iOs - tips and hints?

关于iphone - 释放 CGContextDrawPDFPage 使用的内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5219017/

相关文章:

iphone - 单击 TableView 的单元格时,如何将数据从一个 TableView 传递到另一个 View ?

objective-c - 如何在 tableView :viewForFooterInSection 中添加 UIButton 或 UISwitch

objective-c - 在 iOS 和 Cocoa Touch 上,我们是否应该假设我们应该始终发送 "super"相同的消息?

ios - Xcode 10b5 - 重复符号链接(symbolic link)器错误,无法使用 Crashlytics 进行编译

ios - Swift:无法使用 'filter' 类型的参数列表调用 '((AnyObject) throws -> Bool)'

ios - Swift:将 "Back"作为默认按钮文本设置为 "segue back"按钮

iphone - 重置警报的推送通知设置

iphone - 隐藏 UIToolbar UIBarButtonItems

iOS:主页/电源按下事件

objective-c - self 管理/释放对象在 Xcode -> 产品 -> 分析下报告为 "Potential leak of object"