2014-12-04 58 views
4

我需要用圖像陣列生成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]]; 

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

但它對我沒有幫助。

回答

0

試着這麼做:

[self setupPDFDocumentNamed:@"NewPDF" Width:850 Height:1100]; 

[self beginPDFPage]; 

CGRect textRect = [self addText:@"This is some nice text here, don't you agree?" 
         withFrame:CGRectMake(kPadding, kPadding, 400, 200) fontSize:48.0f]; 

CGRect blueLineRect = [self addLineWithFrame:CGRectMake(kPadding, textRect.origin.y + textRect.size.height + kPadding, _pageSize.width - kPadding*2, 4) 
            withColor:[UIColor blueColor]]; 

UIImage *anImage = [UIImage imageNamed:@"tree.jpg"]; 
CGRect imageRect = [self addImage:anImage 
          atPoint:CGPointMake((_pageSize.width/2)-(anImage.size.width/2), blueLineRect.origin.y + blueLineRect.size.height + kPadding)]; 

[self addLineWithFrame:CGRectMake(kPadding, imageRect.origin.y + imageRect.size.height + kPadding, _pageSize.width - kPadding*2, 4) 
      withColor:[UIColor redColor]]; 

[self finishPDF]; 

給你一些情況下,finishPDF,setupPDFDocumentNamed,beginPDFPage,addImage和addText都是工作方法,是我下面的連接教程的一部分。覺得這是太多轉儲到一個SO回答...

更對這裏 - http://code.tutsplus.com/tutorials/generating-pdf-documents--mobile-11265

相關問題