如何在ios中生成包含图像数组的单页pdf文件?

时间:2014-12-04 13:23:05

标签: ios

我需要生成一个带有图像数组的PDF文件文件我尝试了很多类型的代码,但我没有得到解决方案可以让任何人帮助我。

我尝试了以下代码。

- (void)createPDFWithImagesArray:(NSMutableArray *)array andFileName:(NSString *)fileName
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *PDFPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.pdf",fileName]];

UIGraphicsBeginPDFContextToFile(PDFPath, CGRectZero, nil);
for (UIImage *image in array)
{
    // Mark the beginning of a new page.
    UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, image.size.width, image.size.height), nil);

    [image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
}
UIGraphicsEndPDFContext();
}

但对我没有帮助。

1 个答案:

答案 0 :(得分:0)

尝试类似:

  [self setupPDFDocumentNamed:@"NewPDF" Width:850 Height:1100];

[self beginPDFPage];

CGRect textRect = [self addText:@"This is some nice text here, don't you agree?" 
                      withFrame:CGRectMake(kPadding, kPadding, 400, 200) fontSize:48.0f];

CGRect blueLineRect = [self addLineWithFrame:CGRectMake(kPadding, textRect.origin.y + textRect.size.height + kPadding, _pageSize.width - kPadding*2, 4) 
                                   withColor:[UIColor blueColor]];

UIImage *anImage = [UIImage imageNamed:@"tree.jpg"];
CGRect imageRect = [self addImage:anImage 
                          atPoint:CGPointMake((_pageSize.width/2)-(anImage.size.width/2), blueLineRect.origin.y + blueLineRect.size.height + kPadding)];

[self addLineWithFrame:CGRectMake(kPadding, imageRect.origin.y + imageRect.size.height + kPadding, _pageSize.width - kPadding*2, 4) 
             withColor:[UIColor redColor]];

[self finishPDF];

为了给你一些上下文,finishPDF,setupPDFDocumentNamed,beginPDFPage,addImage和addText都是工作方法,并且都是我在下面链接的教程的一部分。觉得转到一个SO答案太过分了......

更多相关内容 - http://code.tutsplus.com/tutorials/generating-pdf-documents--mobile-11265