如何将CGPDFDocument/CGPDFPage附加到MFMailComposeViewController [英] How to attach CGPDFDocument/CGPDFPage to MFMailComposeViewController
问题描述
我在本地存储器上存储了多页pdf文档.我想从该pdf文档中提取任何页面,并将其转换为NSData以将其附加到"MFMailComposeViewController".使用以下代码行,我可以轻松检索所需的页面...
I have a muti page pdf document stored on local storage. I want to extract any page out of that pdf doc and convert it into NSData to attach it with 'MFMailComposeViewController'. With the following lines of code, I can easily retrive the required page...
CGPDFDocumentRef pdfDoc=CGPDFDocumentCreateWithURL(pdfURL);
CGPDFPageRef pdfPage = CGPDFDocumentGetPage(pdfDoc, pageNumber);
但是我找不到将pdfPage
转换为NSData
的方法,以便可以将其与邮件一起附加.
But I am unable to find a way to convert pdfPage
into NSData
so that I can attach it with mail.
注意:的要求是以PDF格式附加页面,因此请不要建议将PDF转换为PNG或JPEG.
NOTE: The requirement is to attach page in PDF format, so please DON'T suggest converting PDF into PNG or JPEG.
推荐答案
CGPDF
主要用于从PDF绘图到PDF,而不用于转换PDF数据.因此,如果要提取页面,则必须绘制.举例来说:
CGPDF
is primarily for drawing from and to PDF, not for converting PDF data. Therefore, if you want to extract a page, you'll have to draw it. Use for example:
// input
CGDataProviderRef provider = CGDataProviderCreateWithCFData((__bridge CFDataRef)inputData);
CGPDFDocumentRef document = CGPDFDocumentCreateWithProvider(provider);
CGPDFPageRef page = CGPDFDocumentGetPage(document, pageIndex);
CGRect mediaBox = CGPDFPageGetBoxRect(page, kCGPDFMediaBox);
// output
CGDataConsumerRef consumer = CGDataConsumerCreateWithCFData((__bridge CFMutableDataRef)outputData);
CGContextRef context = CGPDFContextCreate(consumer, &mediaBox, NULL);
// draw
CGContextBeginPage(context, &mediaBox);
CGContextDrawPDFPage(context, page);
CGContextEndPage(context);
// cleanup
CGDataProviderRelease(provider);
CGPDFDocumentRelease(document);
CGDataConsumerRelease(consumer);
CGContextRelease(context);
这篇关于如何将CGPDFDocument/CGPDFPage附加到MFMailComposeViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!