如何将整个tableview捕获为图像,从中创建.pdf并通过电子邮件发送 [英] How to capture the whole tableview as an image, create a .pdf from it and email it

查看:68
本文介绍了如何将整个tableview捕获为图像,从中创建.pdf并通过电子邮件发送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次尝试在iOS中创建.pdf文件。
我有一个tableview,它生成我想要在.pdf文件中呈现的所有数据。

this is my first attempt at creating a .pdf file in iOS. I have a tableview that generates all the data I want to be rendered in a .pdf file.

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

This is my code for capturing the whole table as an image, generating the pdf from the image and emailing it:

- (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但它是单页。

I have tested the imageData and the capture is successful. The pdf gets generated but it is a single page.

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

The desired outcome is that the image captured by imageData is used to create a multipage pdf.

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

How should I adapt the 'createPDFfromUIView' method to separate the long image file into multiple pages using A4 standard paper.

任何非常感谢帮助

推荐答案

试试这个而不是 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];

这篇关于如何将整个tableview捕获为图像,从中创建.pdf并通过电子邮件发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆