Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/111.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 从表视图生成PDF时出现问题_Ios_Objective C_Xcode_Uitableview_Pdf - Fatal编程技术网

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

Ios 从表视图生成PDF时出现问题,ios,objective-c,xcode,uitableview,pdf,Ios,Objective C,Xcode,Uitableview,Pdf,我必须从具有多个节和行的UITableView生成PDf 我也生成了pdf,但问题是在创建pdf时,它会剪切某些行的数据并显示在其他页面上 因此,请建议任何动态逻辑,这将有助于在页面上创建PDf数据,而无需转到其他页面 另外,请在下面找到我用来创建PDF的代码 CGRect priorBounds = self.tableView.bounds; CGSize fittedSize = [self.tableView sizeThatFits:CGSizeMake(priorBounds.si

我必须从具有多个节和行的
UITableView
生成PDf

我也生成了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];
CGRect priorBounds=self.tableView.bounds;
cgsizefittedsize=[self.tableView sizehatfits:CGSizeMake(priorBounds.size.width,self.tableView.contentSize.height)];
self.tableView.bounds=CGRectMake(0,0612,fittedSize.height);
CGRect pdfPageBounds=CGRectMake(0,0612792);//根据需要更改此项
NSMutableData*pdfData=[[NSMutableData alloc]init];
UIGraphicsBeginPDFContextToData(pdfData,pdfPageBounds,nil);{
对于(CGFloat pageOriginY=0;pageOriginY

任何帮助都将不胜感激。

在这方面做了大量的研发之后,我终于找到了解决方案。 我知道现在发布上述问题的答案为时已晚,但我正在发布我的解决方案,以便在将来对其他人有所帮助

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

然后我开始通过下面的函数组合细胞图像

- (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];
CGRect priorBounds=self.tableView.bounds;
cgsizefittedsize=[self.tableView sizehatfits: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];
对于(int C=0;C检查高度)
{
[最终添加对象:pdfFinalImage];
pdfFinalImage=[pageArray对象索引:Z];
[最终添加对象:pdfFinalImage];
}否则
{
pdfFinalImage=[通过将image:pdfFinalImage与image:addOnImage组合使用的自映像];
[最终添加对象:pdfFinalImage];
}
}否则
{
UIImage*heightCheckImage=[pageArray对象索引:Z];
如果(PdfinaImage.size.height+heightCheckImage.size.height>checkHeight)
{
[最终添加对象:pdfFinalImage];
pdfFinalImage=[pageArray对象索引:Z];
}否则
{
addOnImage=[pageArray对象索引:Z];
pdfFinalImage=[通过将image:pdfFinalImage与image:addOnImage组合使用的自映像];
}
}
}
UIGraphicsBeginPDFContextToData(pdfData,CGRectMake(0,07681024),nil);
对于(int IC=0;IC<[最终计数];IC++)
{
UIGraphicsBeginPDFPage();
UIImage*mainImage=[finalA objectAtIndex:IC];
NSData*jpegData=UIImageJPEGRepresentation(主图像,0.5);
CGDataProviderRef dp=CGDataProviderCreateWithCFData((_桥CFDataRef)jpegData);
CGImageRef cgImage=CGImageCreateWithJPEGDataProvider(dp、NULL、true、KCGRendingEntentDefault);
[[UIImage imageWithCGImage:cgImage]DrawinRectMake(0,0,mainImage.size.width,mainImage.size.height)];
}
UIGraphicsSendPdfContext();
self.tableView.bounds=priorBounds;
[pdfdatawritetofile:tmpPdfPath原子性:是];

请提出任何解决方案……有解决方案吗?我也有同样的问题。