从webView创建PDF时出现内存泄漏

时间:2016-07-29 07:21:25

标签: ios objective-c pdf memory-leaks

从加载的webView创建pdf时我有内存泄漏。通过UIWebView创建PDF的代码段如下:

- (NSData *)pdfDataForWebView:(UIWebView *)webView {

    UIViewPrintFormatter *formatter =  webView.viewPrintFormatter;
    formatter.startPage = 0;
    UIPrintPageRenderer *render = [[UIPrintPageRenderer alloc] init];
    [render addPrintFormatter:formatter startingAtPageAtIndex:0];

    CGRect page;
    page.origin.x = 0;
    page.origin.y = 0;
    page.size.width = 612;//792;
    page.size.height = 792;//612;

    CGRect printable = CGRectInset(page, 30, 30);
    [render setValue:[NSValue valueWithCGRect:page] forKey:@"paperRect"];
    [render setValue:[NSValue valueWithCGRect:printable] forKey:@"printableRect"];

    NSMutableData * pdfData = [NSMutableData data];
    UIGraphicsBeginPDFContextToData( pdfData, page, nil);
    for (NSInteger i=0; i < [render numberOfPages]; i++) {
        UIGraphicsBeginPDFPage();
        CGRect bounds = UIGraphicsGetPDFContextBounds();
        [render drawPageAtIndex:i inRect:bounds]; //***--Memory-Leak-***
    }
    UIGraphicsEndPDFContext();

    NSString *pdfFile = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/mcsweb.pdf"];
    [pdfData writeToFile:pdfFile atomically:YES];

    NSLog(@"pdf file %@", pdfFile);
    return pdfData;
}

我通过泄漏仪器发现泄漏,但不知道如何解决它。使用断点断点与该特定行一起使用10到20次,而pdf只有2页,然后执行其余代码。

0 个答案:

没有答案