2014-12-08 53 views

回答

3

您可以使用這些方法來生成一個PDF文件,其中包含圖片:

- (void) generatePdfWithFilePath: (NSString *)thefilePath 
{ 
    NSArray *pathsImage = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *docspath = [pathsImage objectAtIndex:0]; 
    NSString *dataPath = [docspath stringByAppendingPathComponent:folder]; 
    NSString *imagepath=[dataPath stringByAppendingPathComponent:imageName]; 

    UIGraphicsBeginPDFContextToFile(thefilePath, CGRectZero, nil); 
    NSInteger currentPage = 0; 
    BOOL done = NO; 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *folder=[NSString stringWithFormat:@"ImageFolder"]; 
    NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:folder]; 

    UIImage *imgBrush = [UIImage imageWithContentsOfFile:imagepath]; 

    for (int i=0; i<directoryContent.count; i++) { 

     [self drawImage:imgBrush]; 

    } 
} 


- (void) drawImage :(UIImage*) imgBrush 
{ 
    if (imgBrush.size.width > pdfpageSize.width || imgBrush.size.height > pdfpageSize.height) { 
     [imgBrush drawInRect:CGRectMake((pdfpageSize.width/2)-(imgBrush.size.width/4), (pdfpageSize.height/2)-(imgBrush.size.height/4), imgBrush.size.width/2, imgBrush.size.height/2)]; 
    } else { 
     [imgBrush drawInRect:CGRectMake((pdfpageSize.width/2)-(imgBrush.size.width/2), (pdfpageSize.height/2)-(imgBrush.size.height/2), imgBrush.size.width, imgBrush.size.height)]; 
    } 

} 

嘿這裏是你可以使用通過代碼來渲染一個PDF圖像的另一種方法:

-(void)drawImagesToPdf:(UIImageView *)button 

{ 
    CGSize pageSize = CGSizeMake(button.frame.size.width*5, button.frame.size.height*5+30); 
    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); 

    UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0.0, pageSize.width, pageSize.height), nil); 

    NSArray *arrImages = [NSArray arrayWithObjects:@"3.png", @"4.png", @"5.png", @"6.png", nil]; 
    float y = 220.0; 

    for (int i=0; i<arrImages.count; i++) { 
     UIImage * myPNG = [UIImage imageNamed:[arrImages objectAtIndex:i]]; 

     [myPNG drawInRect:CGRectMake(50.0, y, myPNG.size.width, myPNG.size.height)]; 

     y += myPNG.size.height + 20; 
    } 

    UIGraphicsEndPDFContext(); 
} 

嘗試我更新的代碼...

希望這對你有幫助... 一切順利.. !!!

4

我稍微改變了以前的解決方案。此方法接受新文件和圖像數組的名稱,並返回pdf的絕對路徑。

- (NSString *)createPdfWithName: (NSString *)name array:(NSArray*)images { 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *docspath = [paths objectAtIndex:0]; 
    NSString *pdfFileName = [docspath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.pdf",name]]; 
    UIGraphicsBeginPDFContextToFile(pdfFileName, CGRectZero, nil); 
    for (int index = 0; index <[images count] ; index++) { 
     UIImage *pngImage=[images objectAtIndex:index];; 
     UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, (pngImage.size.width), (pngImage.size.height)), nil); 
     [pngImage drawInRect:CGRectMake(0, 0, (pngImage.size.width), (pngImage.size.height))]; 
    } 
    UIGraphicsEndPDFContext(); 
    return pdfFileName; 
} 
0

製作實際圖像而不是名稱數組。

NSMutableArray *arr=[[NSMutableArray alloc]initWithObjects:[UIImage imageNamed:@"image1.png"],[UIImage imageNamed:@"image2.png"],nil]; 

使用這一點,並確保您的應用程序包中包含有姓名image1.png和image2.png

圖片
相關問題