Xcode save PDF as image?

I have a question, is it possible to save a PDF file to the Camera Roll? I mean, I know how to save the image, but can you also save the PDF?


source to share

4 answers

If you try to do it indirectly and someone tried it, you won't be able to view it. https://discussions.apple.com/thread/2479252

Camera Roll is for photographs. iBooks is for PDF files.



As @ jshin47 - the following snippet will help you:

This converts the pdf file test.pdf located in the local documents directory to png, called test.png. I used A4 sizes for PDF, but if you are not in UK / Europe you may want US letter sizes.

NSString* localDocuments = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) objectAtIndex:0];
NSString* pdfPath = [localDocuments stringByAppendingPathComponent:@"test.pdf"];

// create a sample PDF (just for illustration)
UIGraphicsBeginPDFContextToFile(pdfPath, CGRectZero, nil);
UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, 612, 792), nil);
[@"Test string" drawInRect:CGRectMake(50, 50, 300, 200) withFont: [UIFont systemFontOfSize: 48.0]];

NSURL* url = [NSURL fileURLWithPath: pdfPath];

CGPDFDocumentRef document = CGPDFDocumentCreateWithURL ((CFURLRef) url);

CGContextRef currentContext = UIGraphicsGetCurrentContext();

CGContextTranslateCTM(currentContext, 0, 842);
CGContextScaleCTM(currentContext, 1.0, -1.0); // make sure the page is the right way up

CGPDFPageRef page = CGPDFDocumentGetPage (document, 1); // first page of PDF is page 1 (not zero)
CGContextDrawPDFPage (currentContext, page);  // draws the page in the graphics context

UIImage* image = UIGraphicsGetImageFromCurrentImageContext();

NSString* imagePath = [localDocuments stringByAppendingPathComponent: @"test.png"];
[UIImagePNGRepresentation(image) writeToFile: imagePath atomically:YES];

UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil); // do error checking in production code




No, It is Immpossible. Apple allows you to save photos and videos to the Photo Library. PDF files can be saved in iBooks or other programs that can handle PDF, but Photo Library cannot accept PDF files



You can convert PDF to a set of images and import them to roll. You cannot add actual PDF to roll.



All Articles