1

例如,如何将在 iPhone 上拍摄的屏幕截图转换为 PDF 文件。截取屏幕截图并将其放入 UIImage 很容易,但让我难倒的是转换。我查看了 Quartz 框架,它是 Xcode 中我知道的唯一支持 PDF 格式的框架,但在那里找不到任何东西来完成这项工作。有什么我缺少的 Xcode 原生的东西吗?如果没有,是否有一个公共框架可以处理这样的转换?

4

2 回答 2

1

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.

于 2012-03-05T14:33:25.207 回答
-1

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

于 2012-03-02T16:49:52.680 回答