简体   繁体   中英

iOS PDF Rendering runs out of memory

I'm using the following code to render about a 100 pages to a pdf in an iOS App, basically creating screenshots of the app's contents:

...
UIGraphicsBeginPDFContextToFile(mainPath, currentFrame, nil);
for(Page *page in pages)
{
  @autoreleasepool
  {
    MainViewController *viewCtrl = [[MainViewController alloc] initWithPage:page contentController:[ContentController singleton] inBackground:NO];
    [self createPageWithView:viewCtrl.view];
  }
}
UIGraphicsEndPDFContext();



-(void)createPageWithView:(UIView *)view
{
  DLog(@"creating page...");
  UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, 0.0);
  [view.layer renderInContext:UIGraphicsGetCurrentContext()];
  UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
  UIGraphicsEndImageContext();

  UIGraphicsBeginPDFPageWithInfo(currentFrame, nil);
  CGContextRef pdfContext = UIGraphicsGetCurrentContext();

  UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
  [imageView.layer renderInContext:pdfContext];
}

This runs out of memory somewhere midways. The mainviewController adds a couple subviews, nothing special. Why is this happening? Shouldnt the autoreleasepool clean up the used memory?

The PDF file is created entirely in memory and written to disk when UIGraphicsEndPDFContext is called. This causes memory problems if the PDF file is getting very large.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM