Reputation: 181
I am trying to use [UIPrintPageRenderer drawPageAtIndex: inRect:] to convert four webViews to a pdf. However, in iOS 8.1.1 it hangs when it reaches this point. There are no error messages, and the code continues to break at this point if I try to continue. Has anyone else seen this problem and found a way around this problem?
When I set the paperRect and printableRect on the renderer, it induces the hang. If I leave those lines out, the app does not hang, but the resulting pdf is blank.
Here is the code (updated):
int i = 0;
UIPrintPageRenderer *renderer = [[UIPrintPageRenderer alloc] init];
for(UIWebView *webView in webViews) {
[renderer addPrintFormatter:webView.viewPrintFormatter startingAtPageAtIndex:i++];
}
CGRect paperRect = CGRectMake(0, 0, 612, 792);
CGRect printableRect = CGRectInset(paperRect, 20, 20);
[renderer setValue:[NSValue valueWithCGRect:paperRect] forKey:@"paperRect"];
[renderer setValue:[NSValue valueWithCGRect:printableRect] forKey:@"printableRect"];
NSMutableData *pdfData = [NSMutableData data];
// Render the html into a PDF
UIGraphicsBeginPDFContextToData( pdfData, CGRectZero, nil );
for (NSInteger i=0; i < [renderer numberOfPages]; i++)
{
UIGraphicsBeginPDFPage();
CGRect bounds = UIGraphicsGetPDFContextBounds();
[renderer drawPageAtIndex:i inRect:bounds]; <-- Hangs here
}
UIGraphicsEndPDFContext();
Upvotes: 3
Views: 714
Reputation: 181
I'm posting this for anyone else who might run into this problem. This hang occurred because exception breakpoints were enabled in Xcode and an exception occurred. The exception was apparently occurring within the implementation of [UIPrintPageRenderer drawPageAtIndex: inRect:]. You can ignore this exception and the code will continue as expected.
Upvotes: 5