CGContextDrawPDFPage Fails When Running from Xcode

Asked 2 years ago, Updated 2 years ago, 93 views

The iOS app is creating a process to convert each page of a PDF file to UIImage.

Up to iOS7, the PDF page was successfully retrieved and converted using the code shown below.
When running in iOS 8.1, you get an unknown cause error during the CGContextDrawPDFPage stage.

The main codes are as follows:

NSURL*url=[NSURL fileURLWithPath:pdfPath];

CGPDFDocumentRef pdf = CGPDFDocumentCreateWithURL(__bridge CFURLRef)url);


CGPDFPageRef pageRef = CGPDFDocumentGetPage(pdf,1);
CGPDFPageRetain (pageRef);

CGRect pageRect = CGPDFPageGetBoxRect (pageRef, kCGPDFMediaBox);

UIGraphicsBeginImageContext (pageRect.size);

CGContextRefcontext=UIGraphicsGetCurrentContext();
CGContextSaveGState (context);

CGContextSetRGBFillColor (context, 1.0, 1.0, 1.0, 1.0);
CGContextFillRect (context, pageRect);
CGContextTranslateCTM (context, 0.0, pageRect.size.height);
CGContextScaleCTM (context, 1.0f, -1.0f);
CGContextSetInterpolationQuality (context, kCGInterpolationHigh);
CGContextSetRenderingIntent(context,kCGRenderingIntentDefault);

CGContextDrawPDFPage (context, pageRef);

CGContextRestoreGState (context);

UIImage* image=UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();
CGPDFPageRelease (pageRef);
CGPDFDocumentRelease (pdf);

Has there been a change in how CoreGraphics handles PDFs from iOS 8?
If you have any information about the cause or hint, please let me know.

In addition, iOS 8 also fails when running from Xcode, but
When I archive and install it on the actual machine with OTA, there are no errors and PDFs can be retrieved and converted successfully.

In other words,
·iOS 8 or higher
·Run from Xcode
·Error only in some PDFs
An error has occurred under the condition that

Regarding the phenomenon where there is a difference between running from Xcode and running from the actual machine alone,
If anyone has any knowledge,
Could you please let me know if it is not directly related to this phenomenon?

ios objective-c xcode iphone pdf

2022-09-30 11:00

1 Answers

After that, I followed the backtrace when the error occurred and searched for the cause.
I understood how to suppress the error by referring to the answers to the questions below.
https://stackoverflow.com/questions/9683547/avaudioplayer-throws-breakpoint-in-debug-mode

To sum up,

throwing C++ region exceptions Ignore C++ exceptions by using exception breakpoints
It seems that



For more information, see

1. Select a row of CGContextDrawPDFPage
Step 1

2. Add exception breakpoint
Step 2

3.Objective-C only?
Step 3

I am writing to let you know that I can do the test without using Archive as soon as possible.
As in this case, if an unknown error occurs due to an exception related to libc++ in the backtrace, you may be able to suppress it in the same way.


However, we do not know the root cause yet, so we will continue to investigate.
Now that I understand that there is a high possibility that the exception in the C++ area is the cause, I will look in that direction.


2022-09-30 11:00

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.