5

生成大型 PDF 时,我的应用程序收到一条内存警告,然后在 PDF 的生成过程中崩溃。PDF 被绘制到网络视图中,当页面超过一定数量(取决于设备)时,我的内存不足

到目前为止,我对此事的研究使我明白我需要:

更改UIGraphicsBeginPDFContextToDataIGraphicsBeginPDFContextToFile

  1. 创建一个合理的临时文件路径,

  2. 把它交给函数,

  3. 将文件交给 webview 加载。

  4. 完成后删除文件。

问题是,虽然我认为我(只是)主要掌握了它,但我不知道如何实现这一点,或者完全理解它以便在我的代码中实现它。在这件事上的建议非常适用

我也愿意接受任何其他想法来阻止内存崩溃

@interface ICPDFPreviewController ()
@property (nonatomic, strong) Certificate *certificate;
@property (nonatomic, strong) NSData *pdfData;
@property (nonatomic) BOOL viewHasUnloaded;
- (void)generatePdf;
- (void)pdfDone:(NSData *)data;
- (NSData *)createPdfWithPages:(NSArray *)pages;
@end

@implementation ICPDFPreviewController
@synthesize certificate=_certificate;
@synthesize scrollView=_scrollView;
@synthesize webView=_webView;
@synthesize pdfData=_pdfData;
@synthesize viewHasUnloaded=_viewHasUnloaded;



- (void)generatePdf
 {
 NSMutableArray *pagesArray = [NSMutableArray array];

 if ([self.certificate.certificateType.title isEqualToString:@"Minor Works"]) {
[pagesArray addObject:[[ICPDFMinorWorksPage1 alloc] initWithCertificate:self.certificate]];
 [pagesArray addObject:[[ICPDFMinorWorksPage2 alloc] initWithCertificate:self.certificate]];

 } else if ([self.certificate.certificateType.title isEqualToString:@"EIC"]) {
[pagesArray addObject:[[ICPDFEICPage1 alloc] initWithCertificate:self.certificate]];
[pagesArray addObject:[[ICPDFEICPage2 alloc] initWithCertificate:self.certificate]];
[pagesArray addObject:[[ICPDFEICPage3 alloc] initWithCertificate:self.certificate]];
[pagesArray addObject:[[ICPDFEICPage4 alloc] initWithCertificate:self.certificate]];
[pagesArray addObject:[[ICPDFEICPage5 alloc] initWithCertificate:self.certificate]];
[self addDistributionBoardsToPagesArray:pagesArray];
ICPDFEICPageFinal *pageFinal = [[ICPDFEICPageFinal alloc]        initWithCertificate:self.certificate];
 pageFinal.pageNumber.text = [NSString stringWithFormat:@"%d", pagesArray.count+1];
[pagesArray addObject:pageFinal];

} else if ([self.certificate.certificateType.title isEqualToString:@"Domestic EIC"]) {
[pagesArray addObject:[[ICPDFDomesticEICPage1 alloc] initWithCertificate:self.certificate]];
[pagesArray addObject:[[ICPDFDomesticEICPage2 alloc] initWithCertificate:self.certificate]];
[pagesArray addObject:[[ICPDFDomesticEICPage3 alloc] initWithCertificate:self.certificate]];
[pagesArray addObject:[[ICPDFDomesticEICPage4 alloc] initWithCertificate:self.certificate]];
[self addDistributionBoardsToPagesArray:pagesArray];
[pagesArray addObject:[[ICPDFDomesticEICPageFinal alloc] initWithCertificate:self.certificate]];

} else if ([self.certificate.certificateType.title isEqualToString:@"EICR"]) {
[pagesArray addObject:[[ICPDFEICRPage1 alloc] initWithCertificate:self.certificate]];
[pagesArray addObject:[[ICPDFEICRPage2 alloc] initWithCertificate:self.certificate]];
[self addObservationsToPagesArray:pagesArray];
[self addDistributionBoardsToPagesArray:pagesArray];
[pagesArray addObject:[[ICPDFEICRInspection alloc] initWithCertificate:self.certificate]];
[pagesArray addObject:[[ICPDFEICRInspectionPage1 alloc] initWithCertificate:self.certificate]];
[pagesArray addObject:[[ICPDFEICRInspectionPage2 alloc] initWithCertificate:self.certificate]];
[pagesArray addObject:[[ICPDFEICRInspectionPage3 alloc] initWithCertificate:self.certificate]];
[pagesArray addObject:[[ICPDFEICRPageFinal alloc] initWithCertificate:self.certificate]];
 }

// Set page count on all pages
int pageNumber = 0;
for (ICCertificateComponent *page in pagesArray) {
page.pageNumber.text = [NSString stringWithFormat:@"%d", ++pageNumber];
page.pageCount.text = [NSString stringWithFormat:@"%d", pagesArray.count];
}

 NSData *pdfData = [self createPdfWithPages:pagesArray];
[self performSelectorOnMainThread:@selector(pdfDone:) withObject:pdfData waitUntilDone:YES];

 }

- (void)pdfDone:(NSData *)data
 {
 self.pdfData = data;
[self.webView loadData:self.pdfData MIMEType:@"application/pdf" textEncodingName:@"utf-8"      baseURL:nil];
 [ICUtils removeProgressView];
 }

 - (NSData *)createPdfWithPages:(NSArray *)pages
  {
 // Creates a mutable data object for updating with binary data, like a byte array
 NSMutableData *pdfData = [NSMutableData data];

 ICCertificateComponent *firstPage = [pages objectAtIndex:0];

UIGraphicsBeginPDFContextToData(pdfData, firstPage.contentView.bounds, nil);

 for (int i = 0; i < pages.count; i++) {
 ICCertificateComponent *thisPage = [pages objectAtIndex:i];
 UIGraphicsBeginPDFPageWithInfo(thisPage.contentView.bounds, nil);


 CGContextRef pdfContext = UIGraphicsGetCurrentContext();
 [thisPage.contentView.layer renderInContext:pdfContext];
 }

 UIGraphicsEndPDFContext();

 return pdfData;
}

- (void)addDistributionBoardsToPagesArray:(NSMutableArray *)pagesArray
{
int pageCount = pagesArray.count;
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"createdAt"       ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil]; 
NSArray *boards = [self.certificate.distributionBoards      sortedArrayUsingDescriptors:sortDescriptors];
 for (DistributionBoard *thisBoard in boards) {
DebugLog(@"Creating a board page");
ICPDFDistributionBoard *boardPage = [[ICPDFDistributionBoard alloc]        initWithDistributionBoard:thisBoard];
boardPage.pageNumber.text = [NSString stringWithFormat:@"%d", ++pageCount];
DebugLog(@"Page number is %d", pageCount);
[pagesArray addObject:boardPage];

NSSortDescriptor *circuitDescriptor = [[NSSortDescriptor alloc] initWithKey:@"createdAt"     ascending:YES];
NSArray *circuitDescriptors = [[NSArray alloc] initWithObjects:circuitDescriptor, nil]; 
NSArray *circuits = [thisBoard.circuits sortedArrayUsingDescriptors:circuitDescriptors];

 //int circuitCount = circuits.count;
 ICPDFCircuitDetails *circuitDetails = boardPage.circuitDetails;

int circuitCount = 0;
for (Circuit *thisCircuit in circuits) {
circuitCount++;
if (circuitCount > 16) {
    // Add an extension page
    DebugLog(@"Adding an extension sheet");
    circuitCount = 1;
    ICPDFDistributionBoardExtension *boardExtension = [[ICPDFDistributionBoardExtension  alloc]   initWithDistributionBoard:thisBoard];
    [pagesArray addObject:boardExtension];
    boardExtension.pageNumber.text = [NSString stringWithFormat:@"%d", ++pageCount];
    circuitDetails = boardExtension.circuitDetails;
   }
    NSString *key = [NSString stringWithFormat:@"circuitRow%d", circuitCount];
   ICCircuitRow *circuitRow = [circuitDetails valueForKey:key];
   [circuitRow populateFromCircuit:thisCircuit];
  }
 }
}

调试控制台警告是其中的一个负载,然后崩溃

 2013-02-08 10:38:35.475 iCertifi[5772:907] Received memory warning.
 2013-02-08 10:38:35.477 iCertifi[5772:907] <ICPDFPreviewController: 0x1eb28930>   didReceiveMemoryWarning
4

4 回答 4

5

很久以前,在一个完全不同的平台上,我正在生成 PDF 文件并遇到了这种问题。解决方案是一次生成一个页面(在打开下一个页面进行成像之前关闭每个页面(不是文档))。这对我的多页文档产生了巨大的影响。查看您的代码,这与您在这里面临的问题相同。实际上,您还有一个额外的问题,即您首先以图形方式在内存中构建所有页面数据,然后在内存中构建所有 pdf 页面并且它崩溃了。请阅读下文,了解如何解决问题的详细信息。

在这里阅读

看来您想要的顺序是:

// start pdf document using default page size (CGRectZero)
UIGraphicsBeginPDFContextToFile(pdfFileName, CGRectZero, nil); 

// then loop through your content drawing it one page at a time.
for ( page p in pages )   // or whatever you are cycling over for page content
{
   UIGraphicsBeginPDFPage();   // or UIGraphicsBeginPDFPageWithInfo

  // do your drawing to the page here by drawing to the current graphics context.
  // if you are setting up transforms and such

}
// close out the last page and the document both
UIGraphicsEndPDFContext();

所以这应该做的是将内存中的渲染数据量保持在一次一页(加上文档全局数据的一些开销)。

好的。我查看了您的代码,如果我理解正确的话,您似乎最终在内存中至少有 2 个呈现页面的副本。

这是关键——一次只画一页。您没有显示所有ICPDFEICRPage1这些是什么,但似乎它们是将页面内容呈现给某种 UIView 的对象?这意味着他们拥有整个视图的像素数据(即副本 1)。因此,在渲染任何 pdf 页面之前,您将一次绘制所有页面的每一页都使用内存。然后打开 PDF 页面上下文,将其呈现为NSMutableData(即数据的第二个副本)。

另一个问题是:为什么要渲染到某种内存结构(视图),而不是在打印该页面时仅渲染到每个页面的 pdf 页面图形上下文?我这是因为您有一个 UIView 层次结构,其中包含字段和在 xib 文件中定义的所有内容,因此您不必手动完成所有布局和字符串的绘制等。正确的?

如果是这样,那么要做的就是让你的 pages 数组成为一个数组,它是你想要的对象的某种列表,ICPDFEICR...你想要它们的顺序,而不是实际分配的对象本身。也许为每种可能的页面类型、观察结果、分发板等定义一个枚举。然后您的 pages 数组只是一个整数数组(这些枚举按您希望在文档中显示的顺序排列)。

然后只为当前页面分配实际对象,将其映像到页面,然后在执行下一页之前将其释放。

另外,我假设(希望!)您正在使用 ARC,否则您在此代码中会有很多内存泄漏(!!!)。

希望这足以让您开始走上正确的道路。哦,使用文件技术,你需要给它一个文件路径,然后你只需将该路径作为 file:// url 传递给 webview 以显示。

虽然现在我想起来了,为什么你要使用 UIWebView 来显示 PDF?查看Zooming PDF Viewer示例以获取不尝试将整个 PDF 文档加载到内存中以显示它的替代方法。

于 2013-02-08T05:19:08.710 回答
2

也许这可以帮助您解决问题。请注意,代码包含

  • 创建文件
  • UIGraphicsGetCurrentContext();添加内容[在 after方法中开始绘图
  • 在 web 视图中查看

从按钮动作开始

- (IBAction)buttonPress:(id)sender
{
    [self createPDFData:[self getPDFFileName]];
}
-(NSString *)getPDFFileName
{
    NSString * fileName = @"Info.PDF";
    
    NSArray *arrayPaths =
    NSSearchPathForDirectoriesInDomains(
                                        NSDocumentDirectory,
                                        NSUserDomainMask,
                                        YES);
    NSString * path = [arrayPaths objectAtIndex:0];
    NSString *pdfFileName = [path stringByAppendingPathComponent:fileName];
    NSLog(@"path : %@",path);
    NSLog(@"pdf file name : %@",pdfFileName);
    return pdfFileName;
    
}

-(void)showPDFFile
{
    
    
    UIWebView *  webView = [[UIWebView alloc] initWithFrame:CGRectMake(0,44,1024,748)];
    
    NSURL *url = [NSURL fileURLWithPath:[self getPDFFileName]];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [webView setScalesPageToFit:YES];
    [webView loadRequest:request];
    
    [self.view addSubview:webView];
    
}


-(void)createPDFData:(NSString *)fileName{
    
    CGRect pageFrame = CGRectMake(0,0,768,1024);
    UIGraphicsBeginPDFContextToFile(fileName, CGRectZero, nil);//This starts drawing a pdf to file named "filename"  UIGraphicsBeginPDFPageWithInfo(pageFrame, nil);//For each new page call this
    UIGraphicsGetCurrentContext();
    UIGraphicsBeginPDFPageWithInfo(pageFrame, nil);//Next or new Page
    NSString * timeLabel = [NSString stringWithFormat:@"Some text"];
    [timeLabel drawInRect:CGRectMake(200, 200, 428, 44) withFont:[UIFont boldSystemFontOfSize:10] lineBreakMode:NSLineBreakByWordWrapping alignment:NSTextAlignmentLeft];
    UIGraphicsEndPDFContext();
    [self showPDFFile];
}
于 2013-02-08T06:11:38.433 回答
0

尝试在你的 for 循环中使用 @autoreleasepool

for (int i = 0; i < pages.count; i++) {

// notice this line 
     @autureleasepool 
     {
       ICCertificateComponent *thisPage = [pages objectAtIndex:i];
       UIGraphicsBeginPDFPageWithInfo(thisPage.contentView.bounds, nil);


       CGContextRef pdfContext = UIGraphicsGetCurrentContext();
       [thisPage.contentView.layer renderInContext:pdfContext];
     }
 }
于 2017-03-30T11:35:27.893 回答
0

如果没有其他工作,您可以尝试我的解决方案。它滚动浏览 web 视图并一次将一页添加到 PDF 中。我无法以任何其他方式解决这个问题。这个解决方案的背面是它使用了一个计时器,并且您将在生成 PDF 时在应用程序中看到滚动。

这是代码:

...
    CGPoint savedContentOffset;
    CGRect savedFrame;
    NSMutableData * pdfData;
    int pdfOffset;
    NSTimer *timer;
...

- (void)preparePdf {
    CGFloat height = aWebView.scrollView.contentSize.height;
    CGRect screenRect = [[UIScreen mainScreen] bounds];
    int pageHeight = screenRect.size.height;

    savedContentOffset = aWebView.scrollView.contentOffset;
    savedFrame = aWebView.frame;
    pdfData=[NSMutableData data];

    aWebView.scrollView.contentOffset = CGPointZero;
    aWebView.frame = CGRectMake(aWebView.frame.origin.x,aWebView.frame.origin.y,aWebView.frame.size.width,pageHeight);

    UIGraphicsBeginPDFContextToData(pdfData, aWebView.frame, nil);

    NSLog(@"pageHeight = %d", pageHeight);
    pdfOffset = 0;

    timer = [NSTimer scheduledTimerWithTimeInterval: 0.4
                                             target: self
                                           selector: @selector(printPdfPage:)
                                           userInfo: nil
                                            repeats: YES];
}

- (void) printPdfPage: (NSTimer*) timer {
    CGFloat height = aWebView.scrollView.contentSize.height;
    CGRect screenRect = [[UIScreen mainScreen] bounds];
    int pageHeight = screenRect.size.height;

    NSLog(@"pdfOffset = %d", pdfOffset);
    UIGraphicsBeginPDFPage();
    [aWebView drawViewHierarchyInRect:CGRectMake(0, 0, screenRect.size.width, pageHeight) afterScreenUpdates:YES];

    pdfOffset += pageHeight;
    aWebView.scrollView.contentOffset = CGPointMake(0, pdfOffset);

    if (pdfOffset > height) {
        [timer invalidate];
        NSLog(@"pdf data size: %ld", pdfData.length);
        [self emailPdf: pdfData];
    }
}

- (void) emailPdf: (NSMutableData*) pdfData {

    // finally end the PDF context.
    UIGraphicsEndPDFContext();

    aWebView.scrollView.contentOffset = savedContentOffset;
    aWebView.frame = savedFrame;

    MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
    mailer.mailComposeDelegate = self;
    [mailer setSubject:[@"iPad Screenshot " stringByAppendingString:[self getDate]]];
    [mailer addAttachmentData:pdfData mimeType:@"application/pdf" fileName:[[self getDate] stringByAppendingPathExtension:@"pdf"]];
    NSString *emailBody = @"";
    [mailer setMessageBody:emailBody isHTML:NO];
    [self presentViewController:mailer animated:YES completion:nil];

    if ([SVProgressHUD isVisible]) {
        [SVProgressHUD showSuccessWithStatus:@"Screenshot prepared."];
    }
    pdfData = nil;
    timer = nil;
}
于 2017-11-17T18:39:56.220 回答