将存储在NSMutableArray中的多个UIImages转换为PDF

时间:2014-01-06 08:17:42

标签: ios objective-c pdf uiimage nsmutablearray

我正在将PDF文件的每个页面转换为UIImage并将其存储在NSMutableArray。现在我想将之前的UIImages转换为PDF文件。我能够转换单个图像,但我希望一次转换所有图像,并且应该与之前的PDF文件相同。

我将UIImage转换为PDF页面的代码:

UIImage *image1 = [ UIImage originalSizeImageWithPDFNamed:[NSString stringWithFormat:@"%@%@",fileName,@".pdf"] atPage:i+1 ];
imageData = [NSData dataWithData:UIImagePNGRepresentation(image1)];
[thumbImage addObject:[UIImage imageWithData:imageData]];

如何将整个UIImages转换为单个PDF文件?

1 个答案:

答案 0 :(得分:4)

使用此方法:

- (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();
}