在iOS中创建包含图像数组的单页pdf文件? [英] Creating a single page pdf file with array of images in iOS?
本文介绍了在iOS中创建包含图像数组的单页pdf文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要在iOS中创建一个包含多个图像的单页pdf文件。我使用了这段代码:
I need create a single page pdf file with multiple images in iOS. i used this code:
- (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文件并且出现错误可以让任何人帮助我...
but it is not generating pdf file and getting error can any body help me please...
推荐答案
根据@kanan Vora我用这段代码解决了我的问题
According to @kanan Vora i have solved my issue with this code
{
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();
}
这篇关于在iOS中创建包含图像数组的单页pdf文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文