从表视图生成PDF时出现问题

时间:2015-11-04 06:38:37

标签: ios objective-c xcode uitableview pdf

我必须从UITableView生成具有多个部分和行的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];

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:2)

在做了很多R&amp; 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];