【问题标题】:Crash app when renderInContext multiple timerenderInContext 多次崩溃应用程序
【发布时间】:2014-07-23 10:09:33
【问题描述】:

我花了很多时间在谷歌搜索,但我没有得到好的结果。 我将 UIWebView 转换为 PDF,这是一个长 contentsize 的 webview(webview 中滚动的 ContentSize)。

我使用 RenderInContext 进行循环,但是当我使用 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 并将结果保存到文件中。但它也会产生崩溃。

【问题讨论】:

    标签: ios objective-c webview uiwebview


    【解决方案1】:

    试试下面我测试过的对我来说很好用的代码……

     -(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];
        }
    

    【讨论】:

    • 感谢您的回答。你的代码和我的代码一样。如果您在 20 时间内在 for 循环中 renderInContext。它使您的应用程序崩溃。我确保我用上面的代码测试了更多时间我知道的是 renderInContext 不会在循环中释放内存。这里最大的问题是如何在使用 renderInContext 一次后释放上下文?我使用了 ARC 并尝试使用:CGContextRelease(currentContext)。但它不能工作 其他最好的主意?请帮帮我
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    • 2018-09-05
    • 1970-01-01
    • 2016-03-14
    相关资源
    最近更新 更多