2014-12-05 47 views
1

我需要在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文件,並收到錯誤任何機構可以幫我請...

+1

有什麼錯誤? – NobodyNada 2014-12-05 05:18:24

+1

[1094:190271 ***終止應用程序由於未捕獲的異常「NSInvalidArgumentException」,原因是:「 - [__ NSArrayM大小]:無法識別的選擇發送到實例0x15e84a70」 – 2014-12-05 05:35:39

+0

確定該應用在這個方法塊崩潰? – Esha 2014-12-05 05:38:48

回答

1

根據@kanan沃拉我已經解決了我的問題與此代碼

{ 

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

} 
相關問題