2014-12-02 15 views
2

我想用[UIPrintPageRenderer drawPageAtIndex:inRect:]將四個webViews轉換爲pdf。但是,在iOS 8.1.1中,它會在到達這一點時掛起。沒有錯誤消息,並且如果我嘗試繼續,代碼會繼續中斷。有沒有其他人看到這個問題,並找到解決這個問題的方法?drawPageAtIndex掛在iOS 8.1.1

當我在渲染器上設置paperRect和printableRect時,它會引起掛起。如果我離開這些線路,應用程序不會掛起,但生成的pdf是空白的。

下面是代碼(更新):

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(); 
+0

哇是啊,它確實...你正試圖在一個緊密的循環內的主線程上執行一個相當密集的功能。嘗試使用NSOperationQueue。 – 2014-12-02 19:59:54

+0

我更新了上面的代碼。上面的代碼在iOS 7.1中運行良好。在iOS 8.1中,代碼掛起。如果我刪除了在渲染器上設置paperRect和printableRect的調用,則代碼不會在iOS 8.1中掛起,但生成的pdf爲空。 – DCG 2014-12-02 21:12:45

回答

4

我張貼這種爲別人誰可能會遇到這個問題。發生這種掛起是因爲在Xcode中啓用了異常斷點併發生異常。這個異常顯然是在[UIPrintPageRenderer drawPageAtIndex:inRect:]的實現中發生的。您可以忽略此異常,並且代碼將按預期繼續。

+0

哇,真是一種解脫,這是爲了殺死PDF渲染給我的:-) – WrightsCS 2015-09-14 04:18:31

相關問題