title: Memory Pressure Crash When Generating PDF
body: <p>My app takes in the contents of a UIWebView and generates a PDF of the web page. This works fine on smaller pages but when it reaches about 10 pages it crashes "Due to Memory Pressure". Also, this is an ARC app.</p>
  
  <p>The predominant answer I have seen is to use <code>UIGraphicsBeginPDFContextToFile</code> instead of <code>UIGraphicsBeginPDFContextToData</code> and after changing to use <code>File</code> I still get a Memory Pressure crash. I don't understand why it's not clearing the pages from memory. I also added the <code>@autoreleasepool { ... }</code> in the loop as recommended in another question. Any ideas on what I am doing wrong here?</p>
  
  <p>Here's the PDF creation code:</p>
  
  <pre><code>UIGraphicsBeginPDFContextToFile(dataFile, CGRectZero, nil);
  
  for (int i = 0; i &lt; 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 &gt; 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();
  </code></pre>
  
  <p>Here's the full method if it helps any:</p>
  
  <pre><code>-(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 &lt; 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 &gt; 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;
  }
  </code></pre>