Quartz PDF API导致内存崩溃 [英] Quartz PDF API Causing Out of Memory Crashes

查看:93
本文介绍了Quartz PDF API导致内存崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用适用于iOS的Quartz PDF API遇到了崩溃问题。目前我正在使用SDK 4.0 GM Seed进行编译并在我的3.2 iPad上运行(我尝试使用3.2 SDK,结果相同)。

I'm having crashing issues using the Quartz PDF API for iOS. At the moment I am compiling with the SDK 4.0 GM Seed and running on my 3.2 iPad (I have tried using the 3.2 SDK with identical results).

所有代码我我正在使用的是基于标准的Apple Quartz文档和来自互联网的各种来源。所以我无法想象我正在做一些完全不同或错误的事情。

All the code I am using is based on the standard Apple Quartz documentation and from various sources around the internets. So I can't image I'm doing something drastically different or wrong.

代码在Simulator中完美运行(所有版本,它是一个通用应用程序),甚至在使用模拟内存警告功能。我使用了泄漏工具,它没有发现任何泄漏。构建和分析也找不到任何东西。我的库中没有留下崩溃或内存不足的日志。

The code runs perfectly in the Simulator (all versions, it's a Universal app) and even while using the "Simulate Memory Warning" function. I've used the Leaks tool and there are no leaks that it finds. Build and Analyze also finds nothing. No crash or low memory log is left in my Library.

所有这些让我相信设备内存不足。这通过说50个pdf页面后发生,大约35%具有某种形式的图像(一些整页图标)。它不会在任何特定页面上崩溃。我加载的pdf大约是75页和3.5MB。

All this leads me to believe the device is running out of memory. This happens after running through say 50 pdf pages, with about 35% having an image of some sort (some full page some icon). It does not crash on any particular page. The pdf I am loading is about 75 pages and 3.5MB.

我在这个网站和互联网上仔细阅读了类似的问题,并且已经应用​​了一些建议下面的代码。我现在在每个页面转向发布pdf文档引用,我不再保留/发布页面引用。我还简化了从使用CGImages到使用UIGraphicsGetImageFromCurrentImageContext函数的图像交换。我尝试了各种切换图像的实现,包括用新分配的临时实例完全替换pdfImgView(使用 [[UIImageView alloc] iniWithImage:UIGraphicsGetImageFromCurrentImageContext()] ),使用setffor pdfImgView并释放temp。所有的变化都通过了Leaks和Analyzer测试,但仍然表现出相同的崩溃行为。

I've perused similar issues on this site and around the internets, and have applied some of the advice in the code below. I now release the pdf document reference on every page turn and I no longer retain/release a page reference. I've also simplified the image swapping from using CGImages to just using the UIGraphicsGetImageFromCurrentImageContext function. I've tried various implementations for switching the images, including replacing the pdfImgView completely with a newly allocated temp instance (using [[UIImageView alloc] iniWithImage:UIGraphicsGetImageFromCurrentImageContext()]), using the setter for pdfImgView and releasing the temp. All of the variations pass the Leaks and Analyzer tests, but still exhibit the same crashing behavior.

所以,在我完全放弃PDF之前,我应该尝试一下或者我缺少的东西?

So, before I move away from PDFs altogether, is there something I should try or something I am missing?

查看在交换页面和首次加载时在界面处理程序中调用的控制器代码:

View controller code that is called in interface handlers to swap pages and on first load:

[self drawPage];

// ...animation code...simple CATransition animation...crashes with or without

// scrollView is a UIScrollView that is a subview of self.view
[scrollView.layer addAnimation:transition forKey:nil];
// pdfImgView is a UIImageView that is a subview of scrollView
pdfImgView.image = UIGraphicsGetImageFromCurrentImageContext();

用于配置和绘制PDF页面到上下文的drawPage方法:

drawPage method used to configure and draw PDF page to the context:

[CFURLRef pdfURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), CFSTR("BME_interior.pdf"), NULL, NULL);
pdfRef = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL); // instance variable, not a property
CFRelease(pdfURL);
CGPDFPageRef page = CGPDFDocumentGetPage(pdfRef, currentPage);

CGRect box = CGPDFPageGetBoxRect(page, kCGPDFMediaBox);
// ...setting scale and imageHeight, both floats...

if (UIGraphicsBeginImageContextWithOptions != NULL) {
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(self.view.frame.size.width, imageHeight), NO, 0.0);
} else {
    UIGraphicsBeginImageContext(CGSizeMake(self.view.frame.size.width, imageHeight));
}
CGContextRef context = UIGraphicsGetCurrentContext();
NSLog(@"page is %d, context is %d, pdf doc is %d, pdf page is %d", currentPage, context, pdfRef, page); // all prints properly

// ...setting up scrollView for new page, using same instance...

CGContextTranslateCTM(context, (self.view.frame.size.width-(box.size.width*scale))/2.0f, imageHeight);
CGContextScaleCTM(context, scale, -1.0*scale);

CGContextSaveGState(context);
CGContextDrawPDFPage(context, page);
CGContextRestoreGState(context);

CGPDFDocumentRelease(pdfRef);
pdfRef = NULL;


推荐答案

啊哈!我在开始新的图像上下文之前通过添加 UIGraphicsEndImageContext(); 修复了崩溃。我现在甚至没有得到记忆警告......

Aha! I've fixed the crashes by adding a UIGraphicsEndImageContext(); before beginning a new image context. I don't even get memory warnings now...

这篇关于Quartz PDF API导致内存崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆