【发布时间】:2014-12-08 05:36:26
【问题描述】:
您好,我需要生成一个包含多个图像数组的多页 pdf 文件,有人可以帮我提供示例代码吗?
【问题讨论】:
标签: ios iphone pdf-generation
您好,我需要生成一个包含多个图像数组的多页 pdf 文件,有人可以帮我提供示例代码吗?
【问题讨论】:
标签: ios iphone pdf-generation
我稍微改变了之前的解决方案。此方法接受新文件的名称和图像数组,并返回 pdf 的绝对路径。
- (NSString *)createPdfWithName: (NSString *)name array:(NSArray*)images {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docspath = [paths objectAtIndex:0];
NSString *pdfFileName = [docspath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.pdf",name]];
UIGraphicsBeginPDFContextToFile(pdfFileName, CGRectZero, nil);
for (int index = 0; index <[images count] ; index++) {
UIImage *pngImage=[images objectAtIndex:index];;
UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, (pngImage.size.width), (pngImage.size.height)), nil);
[pngImage drawInRect:CGRectMake(0, 0, (pngImage.size.width), (pngImage.size.height))];
}
UIGraphicsEndPDFContext();
return pdfFileName;
}
【讨论】:
您可以使用这些方法生成包含图像的 PDF 文件:
- (void) generatePdfWithFilePath: (NSString *)thefilePath
{
NSArray *pathsImage = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docspath = [pathsImage objectAtIndex:0];
NSString *dataPath = [docspath stringByAppendingPathComponent:folder];
NSString *imagepath=[dataPath stringByAppendingPathComponent:imageName];
UIGraphicsBeginPDFContextToFile(thefilePath, CGRectZero, nil);
NSInteger currentPage = 0;
BOOL done = NO;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *folder=[NSString stringWithFormat:@"ImageFolder"];
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:folder];
UIImage *imgBrush = [UIImage imageWithContentsOfFile:imagepath];
for (int i=0; i<directoryContent.count; i++) {
[self drawImage:imgBrush];
}
}
- (void) drawImage :(UIImage*) imgBrush
{
if (imgBrush.size.width > pdfpageSize.width || imgBrush.size.height > pdfpageSize.height) {
[imgBrush drawInRect:CGRectMake((pdfpageSize.width/2)-(imgBrush.size.width/4), (pdfpageSize.height/2)-(imgBrush.size.height/4), imgBrush.size.width/2, imgBrush.size.height/2)];
} else {
[imgBrush drawInRect:CGRectMake((pdfpageSize.width/2)-(imgBrush.size.width/2), (pdfpageSize.height/2)-(imgBrush.size.height/2), imgBrush.size.width, imgBrush.size.height)];
}
}
嘿,这是另一种方法,您可以使用它通过代码在 PDF 中呈现图像:
-(void)drawImagesToPdf:(UIImageView *)button
{
CGSize pageSize = CGSizeMake(button.frame.size.width*5, button.frame.size.height*5+30);
NSLog(@"page size %@",NSStringFromCGSize(pageSize));
NSString *fileName = @"Demo.pdf";
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pdfFileName = [documentsDirectory stringByAppendingPathComponent:fileName];
UIGraphicsBeginPDFContextToFile(pdfFileName, CGRectMake(0, 0, button.frame.size.width, button.frame.size.height*3), nil);
UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0.0, pageSize.width, pageSize.height), nil);
NSArray *arrImages = [NSArray arrayWithObjects:@"3.png", @"4.png", @"5.png", @"6.png", nil];
float y = 220.0;
for (int i=0; i<arrImages.count; i++) {
UIImage * myPNG = [UIImage imageNamed:[arrImages objectAtIndex:i]];
[myPNG drawInRect:CGRectMake(50.0, y, myPNG.size.width, myPNG.size.height)];
y += myPNG.size.height + 20;
}
UIGraphicsEndPDFContext();
}
试试我更新的代码...
希望对你有帮助... 万事如意..!!!
【讨论】:
制作一组实际图像而不是名称。
NSMutableArray *arr=[[NSMutableArray alloc]initWithObjects:[UIImage imageNamed:@"image1.png"],[UIImage imageNamed:@"image2.png"],nil];
使用它并确保您的应用程序包包含名称为 image1.png 和 image2.png 的图像
【讨论】: