0

I am using the following code to convert HTML to PDF.

UIGraphicsBeginPDFContextToFile(self.filePath, CGRectZero, nil);    //creating PDF

    int i = 0;
    for (; i < pages; i++)
    {
        if (pageHeight * (i+1) > height)
        {
            CGRect f = [myWebPage frame];
            f.size.height -= (((i+1) * pageHeight) - height);
            [myWebPage setFrame: f];
        }
        // Specify the size of the pdf page
        UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, width, pageHeight), nil);
        CGContextRef currentContext =  UIGraphicsGetCurrentContext();
        [[[myWebPage subviews] lastObject] setContentOffset:CGPointMake(0, pageHeight * i) animated:NO];
        [myWebPage.layer renderInContext: currentContext];
        NSLog(@"I = %d",i);
    }

UIGraphicsEndPDFContext();
[[[myWebPage subviews] lastObject] setContentOffset:CGPointMake(0, 0) animated:NO];
[myWebPage setFrame:origframe];

but the loop is getting crashed after 10 second or for i >= 12,

its getting crashed at :

 [myWebPage.layer renderInContext: currentContext];

i have tried with "CGContextRelease(currentContext);" also but its not working..

May I please know why is it crashing and how to prevent crashing of app.

Console output:

2013-06-06 10:15:26.179 app[8084:907] Width : 980.000000
2013-06-06 10:15:26.180 app[8084:907] NUMBER OF PAGES : 15
2013-06-06 10:15:26.382 app[8084:907] I = 0
2013-06-06 10:15:28.400 app[8084:907] I = 1
2013-06-06 10:15:28.903 app[8084:907] I = 2
2013-06-06 10:15:30.330 app[8084:907] I = 3
2013-06-06 10:15:31.524 app[8084:907] I = 4
2013-06-06 10:15:32.641 app[8084:907] I = 5
2013-06-06 10:15:33.470 app[8084:907] I = 6
2013-06-06 10:15:34.634 app[8084:907] I = 7
2013-06-06 10:15:35.791 app[8084:907] I = 8
2013-06-06 10:15:36.970 app[8084:907] I = 9
2013-06-06 10:15:38.108 app[8084:907] I = 10
2013-06-06 10:15:38.927 app[8084:907] I = 11 

and app getting crashed in iPhone (Working properly in simulator )

(UPDATED) : and in other case its showing :

2013-06-06 11:17:33.023 app[8202:907] Width : 320.000000
2013-06-06 11:17:33.024 app[8202:907] NUMBER OF PAGES : 2
2013-06-06 11:17:38.200 app[8202:907] I = 0
2013-06-06 11:17:41.390 app[8202:907] I = 1
2013-06-06 11:17:44.028 app[8202:2103] void SendDelegateMessage(NSInvocation *): delegate (webView:didFinishLoadForFrame:) failed to return after waiting 10 seconds. main    run loop mode: kCFRunLoopDefaultMode
(lldb)

and

 libsystem_kernel.dylib`mach_msg_trap:
 0x3aa45e1c:  mov    r12, sp
 0x3aa45e20:  push   {r4, r5, r6, r8}
 0x3aa45e24:  ldm    r12, {r4, r5, r6}
 0x3aa45e28:  mvn    r12, #30
 0x3aa45e2c:  svc    #128
 0x3aa45e30:  pop    {r4, r5, r6, r8}  <=== Thread 1: singal SIGSTOP
 0x3aa45e34:  bx     lr

Please help me out..

4

2 回答 2

1

您的循环阻塞主线程太久。当主线程在 10 秒内没有响应看门狗时,iOS 看门狗进程将终止您的应用程序并出现异常 0x8badfood。

您需要将工作分解为多个部分,以便主线程每 8 或 9 秒有时间与 iOS 通信。或者将工作转移到其他线程。

于 2013-06-06T08:45:40.640 回答
0

AWebView有一个委托协议frameLoadDelegate,它符合WebFrameLoadDelegate

您可以实施的方法之一是

- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame

您的崩溃正在显示

2013-06-06 11:17:44.028 app[8202:2103] void SendDelegateMessage(NSInvocation *): 委托 (webView:didFinishLoadForFrame:) 等待 10 秒后未能返回。主运行循环模式:kCFRunLoopDefaultMode

这表明默认实现可能存在问题

您可能希望将frameLoadDelegateon设置myWebPage为 SomeArbitraryObject (可能是拥有 myWebPage 的对象)并将该方法实现为空

- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
{
//nothing to see here, move along
}
于 2013-06-06T11:01:30.320 回答