我在使用 Core Graphics 库创建 PDF 文件时遇到了一个奇怪的问题。
使用CGContextEndPage方法仅在 iOS 5.1 及更高版本上会发生崩溃 (EXC_BAD_ACCESS),在真实 iPhone 上进行测试。这不会在模拟器上发生。
我可能做错了什么?
更新
PDF方法有300多行。每次创建新页面时,我都会使用 CGContextBeginPage (pdfContext, &pageRect)。奇怪的是,在模拟器上测试时,没有崩溃。
更新 2
// This code block sets up our PDF Context so that we can draw to it
CGContextRef pdfContext;
CFStringRef path;
CFURLRef url;
CFMutableDictionaryRef myDictionary = NULL;
// Create a CFString from the filename we provide to this method when we call it
path = CFStringCreateWithCString (NULL, filename, kCFStringEncodingUTF8);
// Create a CFURL using the CFString we just defined
url = CFURLCreateWithFileSystemPath (NULL, path, kCFURLPOSIXPathStyle, 0);
CFRelease (path);
// This dictionary contains extra options mostly for 'signing' the PDF
myDictionary = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
CFDictionarySetValue(myDictionary, kCGPDFContextTitle, title);
CFDictionarySetValue(myDictionary, kCGPDFContextCreator, CFSTR("MindScore"));
// Create our PDF Context with the CFURL, the CGRect we provide, and the above defined dictionary
pdfContext = CGPDFContextCreateWithURL (url, &pageRect, myDictionary);
// Cleanup our mess
CFRelease(myDictionary);
CFRelease(url);
for (…) {
/** START NEW PAGE */
if (…) {
CGContextEndPage (pdfContext);
}
/** START INITIALIZATION OF PAGE*/
if (…) {
CGContextBeginPage (pdfContext, &pageRect);
// This code block will create an image that we then draw to the page
const char *picture = "pdf-sheet";
CGImageRef image;
CGDataProviderRef provider;
CFStringRef picturePath;
CFURLRef pictureURL;
picturePath = CFStringCreateWithCString (NULL, picture,
kCFStringEncodingUTF8);
pictureURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), picturePath, CFSTR("png"), NULL);
CFRelease(picturePath);
provider = CGDataProviderCreateWithURL (pictureURL);
//CFRelease (pictureURL);
image = CGImageCreateWithPNGDataProvider (provider, NULL, true, kCGRenderingIntentDefault);
CGDataProviderRelease (provider);
CGContextDrawImage (pdfContext, CGRectMake(0, 0, 2480, 3508),image);
CGImageRelease (image);
// End image code
}
}
[...]
/** END LAST PAGE */
NSLog(@"END LAST PAGE");
CGContextEndPage (pdfContext);
CGContextRelease (pdfContext);