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

我需要在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文件并且获取错误可以让任何身体帮助我…

根据@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