Question

How might one convert a screenshot, taken on an iPhone for example, into a PDF file. It's easy enough to take the screenshot and put it into a UIImage, but it's the conversion which has me stumped. I took a look at the Quartz framework which is the only one in Xcode that I know to support the PDF format, but couldn't find anything there to make this work. Is there anything native to Xcode that I'm missing? If not, is there a public framework somewhere that could handle a conversion like this?

Was it helpful?

Solution

I figured it out. It involved saving a screenshot as a UIImage, and then using the very helpful tutorial found here to get me going with the PDF conversion. As it turns out, there are functions to handle the making of PDF documents in the Core Graphics class.

OTHER TIPS

You wouldn't convert a screenshot to a pdf. You would create a screenshot, create a pdf and insert the screenshot image into the first page of the pdf.

Untested code to create image:

nameStr = [NSString stringWithFormat:@"myImage.png"];
fileManager = [NSFileManager defaultManager];
paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
documentsDirectory = [paths objectAtIndex:0];
namePath = [documentsDirectory stringByAppendingString:@"/"];
namePath = [thumbnailPath nameStr];

CGRect contextRect  = CGRectMake(0, 0, 1024, 768);
UIGraphicsBeginImageContext(contextRect.size);  
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

NSData *data = UIImagePNGRepresentation(viewImage);
UIImage *myImage    = [[UIImage alloc] initWithData:data];

Untested code to add image to a pdf:

pageSize = CGSizeMake(1024, 768);
fileName = @"myFile.pdf";
pdfFileName = [documentsDirectory stringByAppendingPathComponent:fileName];

UIGraphicsBeginPDFContextToFile(pdfFileName, CGRectZero, nil);
UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, pageSize.width, pageSize.height), nil);

[myImage drawInRect:CGRectMake( 0, 0, 1024, 768];
UIGraphicsEndPDFContext();

p.s. assuming ipad 1024 x 768 above

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top