我使用以下代码将HTML转换为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];
但是循环在10秒后崩溃或者i> = 12,
它正在崩溃: [myWebPage.layer renderInContext: currentContext];
我试过“CGContextRelease(currentContext);”但它不起作用..
请知道为什么会崩溃以及如何防止应用程序崩溃。
控制台输出:
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
和app在iPhone中崩溃(在模拟器中正常工作)
(更新): 而在其他情况下它显示:
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)
和
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
请帮帮我..
答案 0 :(得分:1)
你的循环阻塞主线程的时间太长了。当主线程没有响应看门狗10秒钟时,iOS看门狗进程将使用例外0x8badfood终止您的应用程序。
你需要将工作分解成碎片,这样主线程每8或9秒就有时间与iOS通信。或者将工作转移到其他线程。
答案 1 :(得分:0)
WebView
的委托协议frameLoadDelegate
符合WebFrameLoadDelegate
您可以实施的方法之一是
- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
您的崩溃正在显示
2013-06-06 11:17:44.028 app [8202:2103]无效 SendDelegateMessage(NSInvocation *):委托 (webView:didFinishLoadForFrame :)等待10后无法返回 秒。主运行循环模式:kCFRunLoopDefaultMode
这表明可能是默认实现的问题
您可能希望将frameLoadDelegate
设置为myWebPage
到SomeArbitraryObject(可能是拥有myWebPage的对象)并将该方法实现为空
- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
{
//nothing to see here, move along
}