gpt4 book ai didi

iphone - Quartz PDF API 导致内存不足崩溃

转载 作者:行者123 更新时间:2023-12-03 21:22:57 24 4
gpt4 key购买 nike

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

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

代码在模拟器(所有版本,它是通用应用程序)中完美运行,甚至在使用“模拟内存警告”功能时也是如此。我使用了 Leaks 工具,它没有发现任何泄漏。构建和分析也什么也没发现。我的库中没有留下任何崩溃或内存不足日志。

所有这些让我相信设备内存不足。这种情况发生在浏览了 50 个 pdf 页面后,大约 35% 的页面有某种类型的图像(有些是整页,有些是图标)。它不会在任何特定页面上崩溃。我正在加载的 pdf 大约有 75 页和 3.5MB。

我仔细研究了本网站和互联网上的类似问题,并应用了下面代码中的一些建议。我现在在每次翻页时都会发布 pdf 文档引用,并且不再保留/发布页面引用。我还简化了图像交换,从使用 CGImages 简化为仅使用 UIGraphicsGetImageFromCurrentImageContext 函数。我尝试了各种切换图像的实现,包括用新分配的临时实例完全替换 pdfImgView (使用 [[UIImageView alloc] iniWithImage:UIGraphicsGetImageFromCurrentImageContext()] ),使用 pdfImgView 的 setter 和释放温度。所有变体都通过了泄漏和分析器测试,但仍然表现出相同的崩溃行为。

那么,在我完全放弃 PDF 之前,有什么我应该尝试的东西或者我缺少的东西吗?

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

[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 方法用于配置 PDF 页面并将其绘制到上下文:

[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(); 修复了崩溃。我现在甚至没有收到内存警告...

关于iphone - Quartz PDF API 导致内存不足崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3088376/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com