iphone - Xcode 将 PDF 保存为图像?

标签 iphone image

我有一个问题,是否可以将 PDF 保存到相机胶卷?我的意思是,我知道如何保存图片,但是你也可以保存 PDF 吗?

最佳答案

作为@jshin47 - 以下片段将帮助您:

这会将本地文档目录中名为 test.pdf 的 pdf 格式转换为名为 test.png 的 png 格式。我在 PDF 中使用了 A4 尺寸,但如果您不在英国/欧洲,则可能需要美国信纸尺寸。

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]];
UIGraphicsEndPDFContext();

NSURL* url = [NSURL fileURLWithPath: pdfPath];

CGPDFDocumentRef document = CGPDFDocumentCreateWithURL ((CFURLRef) url);

UIGraphicsBeginImageContext(CGSizeMake(596,842));
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();
UIGraphicsEndImageContext();

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

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

关于iphone - Xcode 将 PDF 保存为图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9810209/

相关文章:

iphone - 是否有类似 "Codea"的应用程序,但用于 MonoTouch?

iphone - iOS全局方法

iOS 内存分配——一个应用程序可以使用多少内存?

android - 我们如何在android中动态地将图像从网络加载到ViewFlipper?

image - Matlab 图像处理工具包的替代品

javascript - 单击箭头更改图像

ios - 将IOS数据保存到plist

php - 如何将图像从文件夹保存到数据库

c# - 如何找出图像的相对路径

ios - 键盘出现时如何滚动 UIScrollView?