title: Loading a PDF on the iPad is way too slow
body: <p>I am developing an application which render a PDF file using CoreGraphics. I am displaying one page of the PDF at a time. In <code>viewDidLoad</code> I have the following code:</p>
  
  <pre><code>NSString *pdfFullPath = [[NSBundle mainBundle] pathForResource:@"catalogue" ofType:@"pdf"];
  pdf = CGPDFDocumentCreateWithURL((__bridge CFURLRef)[NSURL fileURLWithPath:pdfFullPath]);
  
  currentPage = [[_dataObject description] intValue];
  
  [pdfView setImage:[self imageFromPDF:pdf withPageNumber:currentPage withScale:1.5]];
  </code></pre>
  
  <p><code>[_dataObject description]</code> contains the current page number as a string.</p>
  
  <p>Then I have this method which renders the PDF:</p>
  
  <pre><code>- (UIImage *)imageFromPDF:(CGPDFDocumentRef)_pdf withPageNumber:(NSUInteger)pageNumber withScale:(CGFloat)scale
  {
      if(pageNumber &gt; 0 &amp;&amp; pageNumber &lt;= CGPDFDocumentGetNumberOfPages(_pdf))
      {
          CGPDFPageRef pdfPage = CGPDFDocumentGetPage(_pdf, pageNumber);
          CGRect tmpRect = CGPDFPageGetBoxRect(pdfPage,kCGPDFMediaBox);
          CGRect rect = CGRectMake(tmpRect.origin.x, tmpRect.origin.y, tmpRect.size.width * scale, tmpRect.size.height * scale);
          UIGraphicsBeginImageContext(rect.size);
          CGContextRef context = UIGraphicsGetCurrentContext();
          CGContextTranslateCTM(context, 0, rect.size.height);
          CGContextScaleCTM(context, scale, -scale);
          CGContextDrawPDFPage(context, pdfPage);
          UIImage *pdfImage = UIGraphicsGetImageFromCurrentImageContext();
          UIGraphicsEndImageContext();
  
          return pdfImage;
      }
  
      return nil;
  }
  </code></pre>
  
  <p>This seems to be a very slow process which locks the application entirely when <code>imageFromPDF</code> is being called and therefore I am looking for any way to optimize the process so it would be faster. Any ideas?</p>