如何在iOS中以PDF格式保存UIImage?
我想在文档目录中以PDF格式保存我的图像。如何在iOS中以PDF格式保存UIImage?,ios,ipad,Ios,Ipad,我想在文档目录中以PDF格式保存我的图像。 请建议我保存格式。我只知道在iOS中保存PNG和JPEG格式。首先我们计算PDF页面的大小。根据图像大小和我们想要绘制图像的分辨率,页面大小按如下方式计算(为了灵活性,支持不同的水平和垂直分辨率): 现在我们准备创建PDF文件。它可以直接在磁盘或内存中创建。为了灵活性,让我们在内存中创建它 NSMutableData *pdfFile = [[NSMutableData alloc] init]; CGDataConsumerRef pdfConsum
请建议我保存格式。我只知道在iOS中保存PNG和JPEG格式。首先我们计算PDF页面的大小。根据图像大小和我们想要绘制图像的分辨率,页面大小按如下方式计算(为了灵活性,支持不同的水平和垂直分辨率): 现在我们准备创建PDF文件。它可以直接在磁盘或内存中创建。为了灵活性,让我们在内存中创建它
NSMutableData *pdfFile = [[NSMutableData alloc] init];
CGDataConsumerRef pdfConsumer =
CGDataConsumerCreateWithCFData((CFMutableDataRef)pdfFile);
// The page size matches the image, no white borders.
CGRect mediaBox = CGRectMake(0, 0, pageWidth, pageHeight);
CGContextRef pdfContext = CGPDFContextCreate(pdfConsumer, &mediaBox, NULL);
创建PDF上下文后,我们现在可以执行转换:
CGContextBeginPage(pdfContext, &mediaBox);
CGContextDrawImage(pdfContext, mediaBox, [image CGImage]);
CGContextEndPage(pdfContext);
工作完成后,完成PDF文件并发布使用过的对象
CGContextRelease(pdfContext);
CGDataConsumerRelease(pdfConsumer)
PDF文件现在在pdfFile变量中可用。
我将上面的所有代码放在PDFImageConverter实用程序类中的convertImageToPDF:withHorizontalResolution:verticalResolution:static方法中,它可以这样使用:
NSData *pdfData = [PDFImageConverter convertImageToPDF: image
withHorizontalResolution: 300 verticalResolution: 300];
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent: @"Documents/image.pdf"];
[pdfData writeToFile:path atomically:NO];
CGSize pageSize = CGSizeMake(612, 792);
CGRect imageBoundsRect = CGRectMake(50, 50, 512, 692);
NSData *pdfData = [PDFImageConverter convertImageToPDF: image
withResolution: 300 maxBoundsRect: imageBoundsRect pageSize: pageSize];
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent: @"Documents/image.pdf"];
[pdfData writeToFile:path atomically:NO];
有时需要将图像转换为PDF格式,并使用预定义的页面大小,如字母或A4。此外,还可以为图像指定一个最大边界矩形,以便将其放置在页面上的特定位置,而不会离开页面。在这种情况下,必须调整图像大小(提高分辨率),以确保图像适合给定的矩形
double imageWidth = image.size.width * image.scale * 72 / resolution;
double imageHeight = image.size.height * image.scale * 72 / resolution;
double sx = imageWidth / boundsRect.size.width;
double sy = imageHeight / boundsRect.size.height;
// At least one image edge is larger than maxBoundsRect, reduce its size.
if ((sx > 1) || (sy > 1)) {
double maxScale = sx > sy ? sx : sy;
imageWidth = imageWidth / maxScale;
imageHeight = imageHeight / maxScale;
}
// Put the image in the top left corner of the bounding rectangle
CGRect imageBox = CGRectMake(
boundsRect.origin.x, boundsRect.origin.y + boundsRect.size.height - imageHeight,
imageWidth, imageHeight);
有了最后一个框,可以使用本文前半部分中的代码将图像转换为PDF。
新转换方法的代码位于PDFImageConverter实用程序类中的convertImageToPDF:withResolution:maxBoundsRect:pageSize:static方法中,其使用方式如下:
NSData *pdfData = [PDFImageConverter convertImageToPDF: image
withHorizontalResolution: 300 verticalResolution: 300];
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent: @"Documents/image.pdf"];
[pdfData writeToFile:path atomically:NO];
CGSize pageSize = CGSizeMake(612, 792);
CGRect imageBoundsRect = CGRectMake(50, 50, 512, 692);
NSData *pdfData = [PDFImageConverter convertImageToPDF: image
withResolution: 300 maxBoundsRect: imageBoundsRect pageSize: pageSize];
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent: @"Documents/image.pdf"];
[pdfData writeToFile:path atomically:NO];
您还可以下载源代码,首先我们计算PDF页面的大小。根据图像大小和我们想要绘制图像的分辨率,页面大小按如下方式计算(为了灵活性,支持不同的水平和垂直分辨率): 现在我们准备创建PDF文件。它可以直接在磁盘或内存中创建。为了灵活性,让我们在内存中创建它
NSMutableData *pdfFile = [[NSMutableData alloc] init];
CGDataConsumerRef pdfConsumer =
CGDataConsumerCreateWithCFData((CFMutableDataRef)pdfFile);
// The page size matches the image, no white borders.
CGRect mediaBox = CGRectMake(0, 0, pageWidth, pageHeight);
CGContextRef pdfContext = CGPDFContextCreate(pdfConsumer, &mediaBox, NULL);
创建PDF上下文后,我们现在可以执行转换:
CGContextBeginPage(pdfContext, &mediaBox);
CGContextDrawImage(pdfContext, mediaBox, [image CGImage]);
CGContextEndPage(pdfContext);
工作完成后,完成PDF文件并发布使用过的对象
CGContextRelease(pdfContext);
CGDataConsumerRelease(pdfConsumer)
PDF文件现在在pdfFile变量中可用。
我将上面的所有代码放在PDFImageConverter实用程序类中的convertImageToPDF:withHorizontalResolution:verticalResolution:static方法中,它可以这样使用:
NSData *pdfData = [PDFImageConverter convertImageToPDF: image
withHorizontalResolution: 300 verticalResolution: 300];
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent: @"Documents/image.pdf"];
[pdfData writeToFile:path atomically:NO];
CGSize pageSize = CGSizeMake(612, 792);
CGRect imageBoundsRect = CGRectMake(50, 50, 512, 692);
NSData *pdfData = [PDFImageConverter convertImageToPDF: image
withResolution: 300 maxBoundsRect: imageBoundsRect pageSize: pageSize];
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent: @"Documents/image.pdf"];
[pdfData writeToFile:path atomically:NO];
有时需要将图像转换为PDF格式,并使用预定义的页面大小,如字母或A4。此外,还可以为图像指定一个最大边界矩形,以便将其放置在页面上的特定位置,而不会离开页面。在这种情况下,必须调整图像大小(提高分辨率),以确保图像适合给定的矩形
double imageWidth = image.size.width * image.scale * 72 / resolution;
double imageHeight = image.size.height * image.scale * 72 / resolution;
double sx = imageWidth / boundsRect.size.width;
double sy = imageHeight / boundsRect.size.height;
// At least one image edge is larger than maxBoundsRect, reduce its size.
if ((sx > 1) || (sy > 1)) {
double maxScale = sx > sy ? sx : sy;
imageWidth = imageWidth / maxScale;
imageHeight = imageHeight / maxScale;
}
// Put the image in the top left corner of the bounding rectangle
CGRect imageBox = CGRectMake(
boundsRect.origin.x, boundsRect.origin.y + boundsRect.size.height - imageHeight,
imageWidth, imageHeight);
有了最后一个框,可以使用本文前半部分中的代码将图像转换为PDF。
新转换方法的代码位于PDFImageConverter实用程序类中的convertImageToPDF:withResolution:maxBoundsRect:pageSize:static方法中,其使用方式如下:
NSData *pdfData = [PDFImageConverter convertImageToPDF: image
withHorizontalResolution: 300 verticalResolution: 300];
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent: @"Documents/image.pdf"];
[pdfData writeToFile:path atomically:NO];
CGSize pageSize = CGSizeMake(612, 792);
CGRect imageBoundsRect = CGRectMake(50, 50, 512, 692);
NSData *pdfData = [PDFImageConverter convertImageToPDF: image
withResolution: 300 maxBoundsRect: imageBoundsRect pageSize: pageSize];
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent: @"Documents/image.pdf"];
[pdfData writeToFile:path atomically:NO];
您还可以下载源代码您可以使用本教程 然后使用以下命令将pdf文件保存在文档目录中:
//******** Method To Copy Image to Directory ******** //
- (void) copyFileToDocuments:(NSString *)fileURL
{
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *destinationPath = [documentsDirectory stringByAppendingFormat:@"/convertedImage.pdf"];
NSError *error;
[[NSFileManager defaultManager] copyItemAtURL:[NSURL fileURLWithPath:fileURL] toURL:[NSURL fileURLWithPath:destinationPath] error:&error];
}
然后打电话
[self copyFileToDocuments:moviePath];
希望对您有所帮助。您可以使用本教程 然后使用以下命令将pdf文件保存在文档目录中:
//******** Method To Copy Image to Directory ******** //
- (void) copyFileToDocuments:(NSString *)fileURL
{
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *destinationPath = [documentsDirectory stringByAppendingFormat:@"/convertedImage.pdf"];
NSError *error;
[[NSFileManager defaultManager] copyItemAtURL:[NSURL fileURLWithPath:fileURL] toURL:[NSURL fileURLWithPath:destinationPath] error:&error];
}
然后打电话
[self copyFileToDocuments:moviePath];
希望它对您有所帮助。尝试以下两个选项:--尝试以下两个选项:--