【问题标题】:How to capture the whole tableview as an image, create a .pdf from it and email it如何将整个 tableview 捕获为图像,从中创建 .pdf 并通过电子邮件发送
【发布时间】:2026-02-15 09:20:04
【问题描述】:

这是我第一次尝试在 iOS 中创建 .pdf 文件。 我有一个表格视图,可以生成我想要在 .pdf 文件中呈现的所有数据。

这是我将整个表格捕获为图像、从图像生成 pdf 并通过电子邮件发送的代码:

- (IBAction)save:(id)sender {
    // save all table
    CGRect frame = self.tableView.frame;
    frame.size.height = self.tableView.contentSize.height;
    self.tableView.frame = frame;

    UIGraphicsBeginImageContextWithOptions(self.tableView.bounds.size, self.tableView.opaque, 0.0);
    [self.tableView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *saveImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();
    imageData = UIImagePNGRepresentation(saveImage);

    UIImage *image = [UIImage imageWithData:imageData];
    UIImageView *imageView = [[UIImageView alloc]initWithImage:image];


    [self createPDFfromUIView:imageView saveToDocumentsWithFileName:filename];
   }

- (NSMutableData*)createPDFfromUIView:(UIView*)aView saveToDocumentsWithFileName:(NSString*)aFilename
{
    // Creates a mutable data object for updating with binary data, like a byte array
    NSMutableData *pdfData = [NSMutableData data];

    // Points the pdf converter to the mutable data object and to the UIView to be converted
    UIGraphicsBeginPDFContextToData(pdfData, aView.bounds, nil);
    UIGraphicsBeginPDFPage();
    CGContextRef pdfContext = UIGraphicsGetCurrentContext();


    // draws rect to the view and thus this is captured by UIGraphicsBeginPDFContextToData

    [aView.layer renderInContext:pdfContext];

    // remove PDF rendering context
    UIGraphicsEndPDFContext();

    // Retrieves the document directories from the iOS device
    NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);

    NSString* documentDirectory = [documentDirectories objectAtIndex:0];
    NSString* documentDirectoryFilename = [documentDirectory stringByAppendingPathComponent:aFilename];

    // instructs the mutable data object to write its context to a file on disk
    [pdfData writeToFile:documentDirectoryFilename atomically:YES];
    NSLog(@"documentDirectoryFileName: %@",documentDirectoryFilename);
    return pdfData;
}

-(IBAction)back:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

-(IBAction)email:(id)sender{

    MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
    mc.mailComposeDelegate = self;


    UIImage *image = [UIImage imageWithData:imageData];
    UIImageView *imageView = [[UIImageView alloc]initWithImage:image];


    NSMutableData *pdfData = [self createPDFfromUIView:imageView saveToDocumentsWithFileName:filename];
    // Attach an image to the email
    [mc addAttachmentData:pdfData mimeType:@"application/pdf" fileName:filename];
    // Present mail view controller on screen
    [self presentViewController:mc animated:YES completion:NULL];

}

- (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
    switch (result)
    {
        case MFMailComposeResultCancelled:
            NSLog(@"Mail cancelled");
            break;
        case MFMailComposeResultSaved:
            NSLog(@"Mail saved");
            break;
        case MFMailComposeResultSent:
            NSLog(@"Mail sent");
            break;
        case MFMailComposeResultFailed:
            NSLog(@"Mail sent failure: %@", [error localizedDescription]);
            break;
        default:
            break;
    }

    // Close the Mail Interface
    [self dismissViewControllerAnimated:YES completion:NULL];
}

我已经测试了imageData并且捕获成功。生成pdf,但它是单页。

期望的结果是imageData捕获的图像用于创建多页pdf。

我应该如何调整'createPDFfromUIView'方法以使用A4标准纸将长图像文件分成多页。

任何帮助将不胜感激

【问题讨论】:

    标签: ios objective-c uitableview pdf


    【解决方案1】:

    试试这个而不是UIGraphicsBeginImageContextWithOptions

            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);
    
            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
                                    filePathPDF = [documentsPath stringByAppendingPathComponent:@"image.pdf"]; //Add the file name
         BOOL written = [pdfData writeToFile:filePathPDF atomically:YES];
    

    【讨论】:

    • 感谢您的回复!我尝试了你的更改,但它仍然给了我一页长
    • 是的,如果您想要多个页面,请检查已编辑的页面
    • 非常感谢。这行得通,但我怎样才能考虑页面中的边距。如果我改变'pdfPageBound'的高度,那不会改变实际的页面大小吗?
    • 对不起,我没听懂你。你能详细说明你的要求吗? ?
    • 对不起,我会尽力解释得更好。我想为 pdf 设置边距(主要是底部边距/填充)当我查看 pdf 时,一些内容被切成两半,在一个页面上显示一半,在下一个页面上显示一半。我想将其设置为允许在页面底部填充 30 像素或您可能建议的任何其他方式,以便我解决此问题。再次感谢您的宝贵时间
    最近更新 更多