title: Attaching CGPDFPageRef to MFMailComposeViewController
body: <p>I am trying to email a page of a PDF. </p>
  
  <p>Here is what I have tried:</p>
  
  <pre><code>CGPDFDocumentRef SourcePDFDocument = CGPDFDocumentCreateWithURL((CFURLRef)[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"myPDF" ofType:@"pdf"]]); // Create the CGPDFDocument from the URL
  if (SourcePDFDocument == NULL) {
      return nil;
  }
  
  CGPDFPageRef SourcePDFPage = CGPDFDocumentGetPage(SourcePDFDocument, pageNumber);
  CGRect mediaBox = CGPDFPageGetBoxRect(SourcePDFPage, kCGPDFMediaBox);
  NSData *outputData;
  CGDataConsumerRef consumer = CGDataConsumerCreateWithCFData((__bridge CFMutableDataRef)outputData);
  CGContextRef context = CGPDFContextCreate(consumer, &amp;mediaBox, NULL);
  CGContextBeginPage(context, &amp;mediaBox);
  CGContextDrawPDFPage(context, SourcePDFPage);
  CGContextEndPage(context);
  CGDataConsumerRelease(consumer);
  CGContextRelease(context);
  
  NSString *emailSubject = @"Subject";
  NSString *messageBody = @"message";
  
  MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
  mc.mailComposeDelegate = self;
  [mc setSubject:emailSubject];
  [mc setMessageBody:messageBody isHTML:NO];
  [mc addAttachmentData:outputData mimeType:@"application/pdf" fileName:@"thePage.pdf"];
  
  [self presentViewController:mc animated:YES completion:NULL];
  </code></pre>
  
  <p>But it fails, when the email is received there is no attached file</p>
  
  <p>I am also getting this error</p>
  
  <p><img src="https://i.stack.imgur.com/OxOb9.png" alt="error"></p>