如何在iOS中以编程方式将.png图像转换为.pdf
如何将.png格式的图像转换为.pdf文件如何在iOS中以编程方式将.png图像转换为.pdf,ios,objective-c,image,pdf,Ios,Objective C,Image,Pdf,如何将.png格式的图像转换为.pdf文件 -(void)createPdf:(NSImage*)image { PDFDocument *pdf = [[PDFDocument alloc] init]; NSImage *image = [[NSImage alloc] initWithContentsOfFile:fileName]; PDFPage *page = [[PDFPage alloc] initWithImage:image]; [pdf
-(void)createPdf:(NSImage*)image
{
PDFDocument *pdf = [[PDFDocument alloc] init];
NSImage *image = [[NSImage alloc] initWithContentsOfFile:fileName];
PDFPage *page = [[PDFPage alloc] initWithImage:image];
[pdf insertPage:page atIndex: [pdf pageCount]];
[pdf writeToFile:path];
}
使用上述方法如下:
NSImage *image = [[NSImage alloc] initWithContentsOfFile:PATH_OF_YOUR_PNG_FILE];
[self createPdf:image];
使用以下代码从图像创建PDF文件
UIImage* image = [UIImage imageNamed:@"sample.png"];
CGRect frame = CGRectMake(20, 100, 300, 60);
NSString* fileName = @"FileName.PDF";
NSArray *arrayPaths =
NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory,
NSUserDomainMask,
YES);
NSString *path = [arrayPaths objectAtIndex:0];
NSString* pdfFileName = [path stringByAppendingPathComponent:fileName];
// Create the PDF context using the default page size of 612 x 792.
UIGraphicsBeginPDFContextToFile(pdfFileName, CGRectZero, nil);
// Mark the beginning of a new page.
UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, 612, 792), nil);
[image drawInRect:frame];
// Close the PDF context and write the contents out.
UIGraphicsEndPDFContext();
请参考这个-你会发现的!您是否有一个.png文件的单个图像或一组图像要将图像转换为pdf?这里的PDFDocument和PDFPage是什么?OP询问了iPhone的开发情况。