如何在iPhone sdk中将NSData转换为pdf?

Sar*_*nya 0 pdf iphone objective-c nsdata ios

我将网页转换为pdf文件.我做了以下,

NSString *string=[NSString stringWithFormat:@"%@.pdf",[chapersArray objectAtIndex:pageIndex]];
[controller1 addAttachmentData:pdfData mimeType:@"application/pdf" fileName:string];
[self presentModalViewController:controller1 animated:YES];
[controller1 release];
Run Code Online (Sandbox Code Playgroud)

现在我如何将我的NSData转换为pdf并保存在我的应用程序内存中?请帮我提供示例代码或建议.谢谢大家.

Sri*_*aju 14

假设您在此处的文档目录中有pdf.您可以将其更改为实际位置.试试这个 -

//to convert pdf to NSData
NSString *pdfPath = [documentsDirectory stringByAppendingPathComponent:@"test.pdf"]; 
NSData *myData = [NSData dataWithContentsOfFile:pdfPath]; 
Run Code Online (Sandbox Code Playgroud)

基本上使用CGPDFDocumentCreateWithProvider你可以转换NSData为pdf,

//to convert NSData to pdf
NSData *data               = //some nsdata
CFDataRef myPDFData        = (CFDataRef)data;
CGDataProviderRef provider = CGDataProviderCreateWithCFData(myPDFData);
CGPDFDocumentRef pdf       = CGPDFDocumentCreateWithProvider(provider);
Run Code Online (Sandbox Code Playgroud)

完成后不要忘记CFRelease所有未使用的数据.