我在使用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);