【问题标题】:Creating a single page pdf file with array of images in iOS?在 iOS 中创建带有图像数组的单页 pdf 文件?
【发布时间】:2015-02-05 13:54:30
【问题描述】:

我需要在 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 文件并且出现错误,请任何人帮助我...

【问题讨论】:

  • [1094:190271] *** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[__NSArrayM 大小]:无法识别的选择器发送到实例 0x15e84a70”
  • 收到此错误后,我已分配 array=[[NSMutablearray alloc]init];在 for 循环中,但它正在生成空的 pdf 文件..
  • 您确定将数组传递给函数吗?如果你传递一个未初始化的变量,你一定会得到那个错误(无效的 Argunebt .....并且不要在函数内初始化数组,否则你会清除图像。
  • @paulo 我是 IOS 新手,请您发送一些代码..
  • -(void)imagePickerContoller:(SFMultiImagePickerController *)imagePicker didFinishPickingMediaWithInfo:(NSArray *)info { [self dismissViewControllerAnimated:YES completion:nil]; [图片选择添加对象:信息]; NSLog(@" 选择的图像是 %@",imagesselected); [self createPDFWithImagesArray:imagesselected and FileName:@"mypdf"]; } 通过这个函数我调用了这个方法..

标签: ios objective-c


【解决方案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();

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-07
    相关资源
    最近更新 更多