2013-06-06 40 views
0

我正在使用以下代码将HTML转换为PDF。应用程序在10秒后崩溃或执行“renderInContext”12次或更多次

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 

和应用程式得到坠毁在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

什么是错误?哪一行代码是从哪里来的? – rmaddy

+0

该应用程序正在崩溃.. – Raju

+0

其获取崩溃在“[myWebPage.layer renderInContext:currentContext];”.. – Raju

回答

1

您的循环阻塞主线程时间过长。当主线程对看门狗响应10秒钟时,iOS看门狗进程将以异常0x8badfood的方式终止应用程序。

您需要将工作分解成几部分,以便主线程每8或9秒就有时间与iOS交谈。或者将工作移至其他线程。

+0

我试图在2中打破循环..每次10次..但它仍然崩溃..你可以请建议或根据上面的代码给一些示例代码? – Raju

+0

您必须在10秒内从函数返回,以便UIApplication再次获得控制权。你不能只是打破循环,你必须放弃控制。 –

0

一个WebView有一个委托协议frameLoadDelegate这符合WebFrameLoadDelegate

其中之一,你可以实现的方法是

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

你的崩溃是显示

2013-06 -06 11:17:44.028 app [8202:2103] void SendDelegateMessage(NSInv *):代理 (webView:didFinishLoadForFrame :)在等待10秒后返回 秒。主运行的循环模式:kCFRunLoopDefaultMode

这或许显示那里有一个问题与默认实现

您不妨将您的frameLoadDelegatemyWebPage到SomeArbitraryObject(也许拥有myWebPage的对象)和实施方法空

- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame 
{ 
//nothing to see here, move along 
} 
+0

感谢您的回答,但它仍然崩溃.. :( – Raju

相关问题