• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ios - 如何将整个 tableview 捕获为图像,从中创建 .pdf 并通过电子邮件发送

[复制链接]
菜鸟教程小白 发表于 2022-12-12 10:37:07 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

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

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

- (IBAction)saveid)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*)createPDFfromUIViewUIView*)aView saveToDocumentsWithFileNameNSString*)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)backid)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

-(IBAction)emailid)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) mailComposeControllerMFMailComposeViewController *)controller didFinishWithResultMFMailComposeResult)result errorNSError *)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标准纸将长图像文件分成多页。

任何帮助将不胜感激



Best Answer-推荐答案


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

关于ios - 如何将整个 tableview 捕获为图像,从中创建 .pdf 并通过电子邮件发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24425271/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap