在iOS中创建包含图像数组的单页pdf文件?

时间:2014-12-05 05:10:20

标签: ios objective-c

我需要在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]];

 for (UIImage *image in array)
 {


    // Mark the beginning of a new page.
    CGRect pagebounds=CGRectMake(0, 0, image.size.width, image.size.height);

   CGRect imagebounds=CGRectMake(0, 0, image.size.width, image.size.height);

    UIGraphicsBeginPDFContextToFile(PDFPath, pagebounds, nil);
    {
        UIGraphicsBeginPDFPage();
        [image drawInRect:imagebounds];



    }

  }

   UIGraphicsEndPDFContext();
}

但它没有生成pdf文件并且出现错误可以让任何人帮助我...

1 个答案:

答案 0 :(得分:1)

根据@kanan Vora,我已用此代码解决了我的问题

{

 CGSize pageSize = CGSizeMake(button.frame.size.width*5, button.frame.size.height*5+600);
    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);


    NSArray *arrImages = [NSArray arrayWithObjects:@"1.png", @"2.png", @"1.png",@"2.png", @"1.png", nil];
    float y = 220.0;

    for (int i=0; i<arrImages.count; i++) {


           UIImage * myPNG = [UIImage imageNamed:[arrImages objectAtIndex:i]];

        UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0.0, pageSize.width, pageSize.height), nil);






 [myPNG drawInRect:CGRectMake(120.0, y, myPNG.size.width, myPNG.size.height)];




//        y -= myPNG.size.height+1;
    }


    UIGraphicsEndPDFContext();

}