2014-07-23 25 views
0

我在Google搜索上花了很多時間,但是我沒有得到好的結果。 我將UIWebView轉換爲PDF,具有長contentize(webview中滾動的ContentSize)的webview。renderInContext多次崩潰時的應用程序

我使用RenderInContext for循環,但它使崩潰時,我用webview轉換長 請幫助我解決這個錯誤與20頁。

這是我的代碼:

UIGraphicsBeginPDFContextToFile(documentDirectoryFilename, CGRectMake(0, 0, aWebView.scrollView.contentSize.width, screenHight), nil); 
aWebView.scrollView.contentSize.width, screenHight), nil); 

for (int i = 0; i < 20; i++) { 

    @autoreleasepool { 
     UIGraphicsBeginPDFPage(); 
     CGContextRef currentContext = UIGraphicsGetCurrentContext(); 
     [[[aWebView subviews] lastObject] setContentOffset:CGPointMake(0, screenHight * i) animated:NO]; 
     [aWebView.layer renderInContext:currentContext]; 
     currentContext = nil; 
    } 

} 
UIGraphicsEndPDFContext(); 
aWebView.layer.contents = nil; 

我用autoReleasePool並保存結果文件。但它也會導致崩潰。

回答

0

嘗試下面的代碼,我用測試的正常工作對我來說...

-(void)createPDFfromUIView:(UIWebView*)webView saveToDocumentsWithFileName:(NSString*)aFilename 
    { 
     NSString *heightStr = [webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight;"]; 

     int height = [heightStr intValue]; 
     // CGRect screenRect = [[UIScreen mainScreen] bounds]; 
     // CGFloat screenHeight = (self.contentWebView.hidden)?screenRect.size.width:screenRect.size.height; 
     CGFloat screenHeight = webView.bounds.size.height; 
     int pages = ceil(height/screenHeight); 

     NSMutableData *pdfData = [NSMutableData data]; 
     UIGraphicsBeginPDFContextToData(pdfData, webView.bounds, nil); 
     CGRect frame = [webView frame]; 
     for (int i = 0; i < pages; i++) { 
      // Check to screenHeight if page draws more than the height of the UIWebView 
      if ((i+1) * screenHeight > height) { 
       CGRect f = [webView frame]; 
       f.size.height -= (((i+1) * screenHeight) - height); 
       [webView setFrame: f]; 
      } 

      UIGraphicsBeginPDFPage(); 
      CGContextRef currentContext = UIGraphicsGetCurrentContext(); 
      //  CGContextTranslateCTM(currentContext, 72, 72); // Translate for 1" margins 

      [[[webView subviews] lastObject] setContentOffset:CGPointMake(0, screenHeight * i) animated:NO]; 
      [webView.layer renderInContext:currentContext]; 
     } 

     UIGraphicsEndPDFContext(); 
     // Retrieves the document directories from the iOS device 
     NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES); 

     NSString* documentDirectory = [documentDirectories objectAtIndex:0]; 
     NSString* documentDirectoryFilename = [documentDirectory stringByAppendingPathComponent:aFilename]; 

     // instructs the mutable data object to write its context to a file on disk 
     [pdfData writeToFile:documentDirectoryFilename atomically:YES]; 
     [webView setFrame:frame]; 
    } 
+0

謝謝您的回答。你的代碼和我的代碼一樣。如果您在20秒內renderInContext in for循環。它會讓你的應用崩潰。我確定我用上面的代碼測試了更多的時間 我知道的是renderInContext不會在循環中釋放內存。這裏最大的問題是如何在使用renderInContext一次之後釋放上下文?我使用ARC並嘗試使用:CGContextRelease(currentContext)。但它不能工作 其他最好的主意?請幫幫我 –

相關問題