OStack程序员社区-中国程序员成长平台

标题: ios - 从表格 View 生成 PDF 时出现问题 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 16:56
标题: ios - 从表格 View 生成 PDF 时出现问题

我必须从具有多个部分和行的 UITableView 生成 PDf。

image

我也生成了 pdf,但问题是在创建 PDF 时,它会剪切某些行的数据并显示在其他页面上。

因此,请提出任何有助于创建 PDf 的动态逻辑,该 PDf 在页面上包含数据而无需转到其他 Page 。

另外请在下面找到我用来创建 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];

任何帮助将不胜感激。



Best Answer-推荐答案


在这方面做了很多研发,终于得到了解决方案。 我知道这为时已晚,无法发布上述问题的答案,但我正在发布我的解决方案,以便将来对某人有所帮助。

我用来避免这个问题的技巧是,逐节运行循环并拍摄每个动态单元格的照片。

然后我开始通过下面的函数组合单元格的图像,

- (UIImage*)imageByCombiningImageUIImage*)firstImage withImageUIImage*)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];

关于ios - 从表格 View 生成 PDF 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33515228/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4