我遇到了使用Quartz PDF API for iOS崩溃的问题。目前我正在使用SDK 4.0 GM Seed进行编译,并在我的3.2 iPad上运行(我尝试使用3.2 SDK获得相同的结果)。石英PDF API导致内存不足崩溃
我使用的所有代码都基于标准的Apple Quartz文档以及来自各种互联网的资源。所以我无法想象我正在做一些完全不同或错误的事情。
代码在模拟器(所有版本,它是一个通用应用程序),甚至在使用“模拟内存警告”功能时都能够完美运行。我使用了泄漏工具,没有发现泄漏。建立和分析也没有发现。我的库中没有发生崩溃或内存不足日志。
这一切都让我相信设备内存不足。发生这种情况后,通过说50页的pdf页面,约35%有一些图像(一些完整的页面一些图标)。它不会在任何特定页面上崩溃。我正在加载的pdf大约有75页和3.5MB。
我已经仔细阅读了本网站和互联网上的类似问题,并在下面的代码中应用了一些建议。我现在每转一页就发布pdf文档参考,而我不再保留/发布页面参考。我还简化了从使用CGImages到仅使用UIGraphicsGetImageFromCurrentImageContext函数的图像交换。我已经尝试过用于切换图像的各种实现,包括用新分配的临时实例(使用[[UIImageView alloc] iniWithImage:UIGraphicsGetImageFromCurrentImageContext()]
)完全替换pdfImgView,使用pdfImgView的setter和释放temp。所有这些变化都通过了泄漏和分析仪测试,但仍然表现出相同的崩溃行为。
因此,在我完全不用PDF格式之前,有什么我应该尝试的或者我失踪的东西?
,被称作接口处理器交换页和第一次加载视图控制器代码:用于配置和借鉴PDF页面上下文
[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();
一个DrawPage方法:
[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;
您可以使用仪器,看看哪些对象是吃所有的可用内存。 (仪器不仅可以检查泄漏,还可以检查活物。)它们是什么?答案将取决于此。 – Yuji 2010-06-21 22:57:58
我的实时分配永远不会超过〜12MB,然后突然上升然后下降,通常在闲置时平均3-5MB左右。使用内存监视器仪器告诉我,我的应用程序的虚拟内存在崩溃之前超过了200MB。 我看到内存警告1级然后2级,然后几页后它会崩溃。因此,应该释放由UIImage或UIImageView缓存的图像(如果有的话),但显然还是有些东西悬而未决。 – bensnider 2010-06-22 13:16:42