2015-11-04 78 views
2

我必须从UITableView生成具有多个节和行的PDf。从表视图中生成PDF时发生的问题

image

我已经生成的PDF很好,但问题是在创建PDF它正在削减某些行的数据,并显示其他网页上。

所以请建议任何动态逻辑,这将有助于在页面上创建具有数据的PDf而无需转到其他页面。

另请参阅下面我用来创建PDF的代码。

CGRect priorBounds = self.tableView.bounds; 
CGSize fittedSize = [self.tableView sizeThatFits:CGSizeMake(priorBounds.size.width, self.tableView.contentSize.height)]; 
self.tableView.bounds = CGRectMake(0, 0, 612, fittedSize.height); 

CGRect pdfPageBounds = CGRectMake(0, 0, 612, 792); // Change this as your need 
NSMutableData *pdfData = [[NSMutableData alloc] init]; 

UIGraphicsBeginPDFContextToData(pdfData, pdfPageBounds, nil); { 
    for (CGFloat pageOriginY = 0; pageOriginY < fittedSize.height; pageOriginY += pdfPageBounds.size.height) { 
     UIGraphicsBeginPDFPageWithInfo(pdfPageBounds, nil); 

     CGContextSaveGState(UIGraphicsGetCurrentContext()); { 
      CGContextTranslateCTM(UIGraphicsGetCurrentContext(), 0, -pageOriginY); 
      [self.tableView.layer renderInContext:UIGraphicsGetCurrentContext()]; 
     } CGContextRestoreGState(UIGraphicsGetCurrentContext()); 
    } 
} UIGraphicsEndPDFContext(); 

self.tableView.bounds = priorBounds; // Reset the tableView 


// Use the pdfData to 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsPath = [paths objectAtIndex:0]; 
//Get the docs directory 
NSString *filePathPDF = [documentsPath stringByAppendingPathComponent:@"image.pdf"]; //Add the file name 
[pdfData writeToFile:filePathPDF atomically:YES]; 

任何帮助将不胜感激。

+0

请提出解决方案.... – user1487846

+0

这个任何解决方案?我陷入了同样的问题。 – Jorge

回答

2

做了很多R & D,最后我得到了解决方案。 我知道这个太迟了,不能发布上述问题的答案,但我发布了我的解决方案,以便将来可以帮助某人。

我用来避免这个问题的诀窍是,每个部分明智地运行循环并拍摄每个动态单元格的图片。

然后,我开始由以下函数结合细胞的图像,

- (UIImage*)imageByCombiningImage:(UIImage*)firstImage withImage:(UIImage*)secondImage { 
    UIImage *image1 = firstImage; 
    UIImage *image2 = secondImage; 

    CGSize size = CGSizeMake(image1.size.width, image1.size.height + image2.size.height); 

    UIGraphicsBeginImageContext(size); 

    [image1 drawInRect:CGRectMake(0,0,size.width, image1.size.height)]; 
    [image2 drawInRect:CGRectMake(0,image1.size.height,size.width, image2.size.height)]; 

    UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext(); 

    UIGraphicsEndImageContext(); 

    //set finalImage to IBOulet UIImageView 
    return finalImage; 
} 

和以生成组合图像,我用下面的逻辑。在那里我检查了组合图像的高度,如果它大于屏幕尺寸或不是。 下面是我整个的代码逻辑,

CGRect priorBounds = self.tableView.bounds; 
    CGSize fittedSize = [self.tableView sizeThatFits:CGSizeMake(priorBounds.size.width, self.tableView.contentSize.height)]; 
    self.tableView.bounds = CGRectMake(0, 0, fittedSize.width, fittedSize.height); 

    pdfViews = [[NSMutableArray alloc] init]; 
    NSMutableData *pdfData = [[NSMutableData alloc] init]; 

    float sections = [self.tableView numberOfSections]; 
    for (int C = 0; C < sections; C++) 
    { 
     int rows = 0; 
     rows = [self.tableView numberOfRowsInSection:C]; 

     for (int I = 0; I < rows; I++) 
     { 
      NSIndexPath *indexPath = [NSIndexPath indexPathForRow:I inSection:C]; 

      UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath]; 
      UIGraphicsBeginImageContextWithOptions(cell.bounds.size, cell.opaque, 0.0); 
      [cell.layer renderInContext:UIGraphicsGetCurrentContext()]; 
      UIImage *cellImage = UIGraphicsGetImageFromCurrentImageContext(); 
      UIGraphicsEndImageContext(); 
      [pdfViews addObject:cellImage]; 
     } 
    } 

    NSArray *pageArray = pdfViews; 
    UIImage *pdfFinalImage = [[UIImage alloc] init]; 
    UIImage *addOnImage = [[UIImage alloc] init]; 
    NSMutableArray *finalA = [[NSMutableArray alloc] init]; 
    int checkHeight; 
    checkHeight = 1024; 

    for (int Z = 0; Z < [pdfViews count]; Z++) 
    { 
     if (Z == 0) 
     { 
      pdfFinalImage = [pageArray objectAtIndex:Z]; 

     }else if (Z+1 == [pdfViews count]) 
     { 
      addOnImage = [pageArray objectAtIndex:Z]; 
      if (pdfFinalImage.size.height+addOnImage.size.height > checkHeight) 
      { 
       [finalA addObject:pdfFinalImage]; 
       pdfFinalImage = [pageArray objectAtIndex:Z]; 
       [finalA addObject:pdfFinalImage]; 

      }else 
      { 
       pdfFinalImage = [self imageByCombiningImage:pdfFinalImage withImage:addOnImage]; 
       [finalA addObject:pdfFinalImage]; 
      } 

     }else 
     { 
      UIImage *heightCheckImage = [pageArray objectAtIndex:Z]; 
      if (pdfFinalImage.size.height+heightCheckImage.size.height > checkHeight) 
      { 
       [finalA addObject:pdfFinalImage]; 
       pdfFinalImage = [pageArray objectAtIndex:Z]; 
      }else 
      { 
       addOnImage = [pageArray objectAtIndex:Z]; 
       pdfFinalImage = [self imageByCombiningImage:pdfFinalImage withImage:addOnImage]; 
      } 
     } 
    } 

    UIGraphicsBeginPDFContextToData(pdfData, CGRectMake(0, 0, 768, 1024), nil); 
    for (int IC = 0; IC < [finalA count]; IC++) 
    { 
     UIGraphicsBeginPDFPage(); 
     UIImage *mainImage = [finalA objectAtIndex:IC]; 
     NSData *jpegData = UIImageJPEGRepresentation(mainImage, 0.5); 
     CGDataProviderRef dp = CGDataProviderCreateWithCFData((__bridge CFDataRef)jpegData); 
     CGImageRef cgImage = CGImageCreateWithJPEGDataProvider(dp, NULL, true, kCGRenderingIntentDefault); 
     [[UIImage imageWithCGImage:cgImage] drawInRect:CGRectMake(0, 0, mainImage.size.width, mainImage.size.height)]; 
    } 

    UIGraphicsEndPDFContext(); 
    self.tableView.bounds = priorBounds; 
    [pdfData writeToFile:tmpPdfPath atomically:YES];