ios - 生成PDF时出现内存压力崩溃

标签 ios objective-c pdf uiwebview

我的应用程序接收UIWebView的内容,并生成网页的PDF。这在较小的页面上可以正常工作,但是当到达约10页时,由于“内存不足”而崩溃。另外,这是一个ARC应用程序。

我看到的主要答案是使用UIGraphicsBeginPDFContextToFile而不是UIGraphicsBeginPDFContextToData,在更改为使用File之后,我仍然遇到内存压力崩溃的问题。我不明白为什么它没有从内存中清除页面。我还按照另一个问题的建议在循环中添加了@autoreleasepool { ... }。对我在这里做错的任何想法吗?

这是PDF创建代码:

UIGraphicsBeginPDFContextToFile(dataFile, CGRectZero, nil);

for (int i = 0; i < pages; i++) {
    @autoreleasepool {
        NSLog(@"Creating Page %i", i);
        // Check to see if page draws more than the height of the UIWebView
        if ((i+1) * 720 > height) {
            CGRect f = [_appWebView frame];
            f.size.height -= (((i+1) * 720.0) - height);
            [_appWebView setFrame: f];
        }

        UIGraphicsBeginPDFPage();
        CGContextRef currentContext = UIGraphicsGetCurrentContext();
        CGContextTranslateCTM(currentContext, 36, 36); // Translate for 0.5" margins
        [[[_appWebView subviews] lastObject] setContentOffset:CGPointMake(0, 720 * i) animated:NO];
        [_appWebView.layer renderInContext:currentContext];
    }
}
UIGraphicsEndPDFContext();

这是完整的方法,如果有帮助的话:
-(void) generatePDF {

    startingFrame = _appWebView.frame;

    // Memory warning seems to happen on almost every PDF, clear cache here to be proactive.
    [[NSURLCache sharedURLCache] removeAllCachedResponses];

    UIWebView *webView = [[UIWebView alloc] initWithFrame: CGRectMake(0, 0, 6.5 * 72, 9 * 72)];
    [webView setDelegate: self];

    // Adjust to letter size paper size in portrait mode
    CGRect frame = _appWebView.frame;
    frame.size.height = 10*72; // 11" - 1" Margins = 720px (72px / inch)
    frame.size.width = 7.5*72; // 8.5 - 1" Margins = 612px (72px / inch)
    _appWebView.frame = frame;

    [_appWebView stringByEvaluatingJavaScriptFromString:@"window.scroll(0, 0);"];

    // Get the height of our webView
    NSString *heightStr = [_appWebView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight;"];

    int height = [heightStr intValue];

    // Get the number of pages needed to print. 10 * 72 = 720
    int pages = ceil(height / 720.0);

    // File
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *dataFile = [documentsDirectory stringByAppendingPathComponent:@"Configuration.pdf"];
    NSLog(@"File: %@", dataFile);

    UIGraphicsBeginPDFContextToFile(dataFile, CGRectZero, nil);

    for (int i = 0; i < pages; i++) {
        @autoreleasepool {
            NSLog(@"Creating Page %i", i);
            // Check to see if page draws more than the height of the UIWebView
            if ((i+1) * 720 > height) {
                CGRect f = [_appWebView frame];
                f.size.height -= (((i+1) * 720.0) - height);
                [_appWebView setFrame: f];
            }

            UIGraphicsBeginPDFPage();
            CGContextRef currentContext = UIGraphicsGetCurrentContext();
            CGContextTranslateCTM(currentContext, 36, 36); // Translate for 0.5" margins
            [[[_appWebView subviews] lastObject] setContentOffset:CGPointMake(0, 720 * i) animated:NO];
            [_appWebView.layer renderInContext:currentContext];
        }
    }

    UIGraphicsEndPDFContext();

    // Adjust to original size
    _appWebView.frame = startingFrame;
}

最佳答案

我不知道您收到内存错误的原因,但是在搜索PDF时遇到了类似的问题。基本上,操作系统是将整个PDF加载到内存中,然后搜索,然后删除pdf,甚至以为我正在逐页浏览。我的解决方案是一次只做一页,这为我解决了内存问题。

我的代码如下所示:

NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];
operationQueue.MaxConcurrentOperationCount = 1;


for(int i = 1; i <= totalPages; i++)
{

    // a block of operation
    [operationQueue addOperationWithBlock: ^ {


     }];
}

关于ios - 生成PDF时出现内存压力崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19987721/

相关文章:

ios - 使用 id3taggenerator 和 mediafilesegmenter 将定时元数据插入 HLS(HTTP 直播流)

ios - 对日期进行排序,首先是现在,过去在底部

ios - 如何在 Cocos2D 3.x 中为 CCSprite 制作动画?

ios - 使用嵌套上下文时如何自动设置核心数据关系

c# - ASP .NET C# - 将圆形 SqlDataSource 放入方形 DataTable 孔中?

ios - 为什么 hitTest 不返回附加到窗口的 View ?

ios - RestKit:现在 `postObject mapResponseWith: delegate:` 已弃用,我该用什么?

ios - 重新实例化实例变量 - 一个设计查询

android - 在 Android 中启动特定应用

java - Apache POI 转换器,docx 到 pdf 异常(exception)