【问题标题】:Objective-C Full UIView to PDFObjective-C 完整 UIView 到 PDF
【发布时间】:2016-01-03 21:00:29
【问题描述】:

我这里有这段代码,它将我的 UIView 放入 PDF 中。我遇到的问题是我的视图是一个详细视图控制器并且是可滚动的,并且 PDF 只能获取当时详细视图控制器内部的内容而不是完整视图,如果我在详细视图中移动,它将捕获任何内容我在的部分,而不是完整的东西。我正在尝试做的事情可能吗?

- (IBAction)Share:(id)sender {

    NSMutableData *pdfData = [NSMutableData data];

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


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

    [spreadSheet.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:@"test.pdf"];

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


    UIActivityViewController * activityController = [[UIActivityViewController alloc] initWithActivityItems:@[pdfData] applicationActivities:nil];

    UIPopoverController *popup = [[UIPopoverController alloc] initWithContentViewController:activityController];

    [popup presentPopoverFromRect:CGRectMake(self.view.frame.size.width - 36, 60, 0, 0)inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];

}

更新

我一直在为此苦苦挣扎,我无法将完整视图转换为 PDF,我也一直在考虑采用我的 NSMutableArray 和我的 NSArray(这是我的 UIView 中使用的数据)并尝试将其转换为 PDF,但这听起来非常耗时,除非有人知道如何做到这一点。我想我对我应该走哪条路线感到困惑。

【问题讨论】:

  • 那么你想要的其实不是“完整的 UIView”,它是一个 UIView 在一堆不同状态下的编译?您在详细视图控制器中使用什么?哪个班?
  • 您是否尝试过使用电子表格的内容大小而不是边界?
  • “电子表格”的层次结构是什么?如果它是表格视图或集合视图,则没有直接的方法可以做到这一点,因为不可见的单元格甚至还没有呈现。
  • @Aris 在 tableView 或 collection view 的情况下,您可以随时强制渲染所有单元格。
  • 就像你可以彻底循环并调用 [self tableView: self.tableView cellForRowAtIndexPath: indexPath];

标签: ios objective-c pdf uiview


【解决方案1】:

从这里

我遇到的问题是我的视图是一个详细视图控制器,并且 是可滚动的,PDF 只获取详细视图中的内容 当时的控制器,而不是完整的视图。

看起来问题是您在视图中的内容是可滚动的,而您创建的 pdf 仅捕获可见区域。

这是你的问题

UIGraphicsBeginPDFContextToData(pdfData, spreadSheet.bounds, nil);

您需要修复它,您需要为其提供可滚动视图(UITableView、UICollectionView 或 Scrollview)内容大小限制,这就是您的做法。

    -(NSData*)pdfDataFromTableViewContent
{
   NSMutableData *pdfData = [NSMutableData data];
   //The real size, not only the visible part use your scrollable view ((UITableView,UICollectionView or Scrollview))
   CGSize contentSize = self.tableView.contentSize;
   CGRect tableViewRealFrame = self.tableView.frame;
   //Size tableView to show the whole content
   self.tableView.frame = CGRectMake(0, 0, contentSize.width, contentSize.height);
   //Generate PDF (one page)
   UIGraphicsBeginPDFContextToData(pdfData,self.tableView.frame, nil);
   UIGraphicsBeginPDFPage();
   [self.tableView.layer renderInContext:UIGraphicsGetCurrentContext()];
   UIGraphicsEndPDFContext();
   //Resize frame
   self.tableView.frame = tableViewRealFrame;
   return  pdfData;
}

上面的代码会生成一个页面。 更多页面使用以下代码

//MultiPage
CGRect mediaBox = self.tableView.frame;
CGSize pageSize = CGSizeMake(self.view.frame.size.width, 800);
UIGraphicsBeginPDFContextToData(pdfData, CGRectZero, nil);
CGContextRef pdfContext = UIGraphicsGetCurrentContext();
NSInteger currentPage = 0;
BOOL done = NO;
do {
    UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0.0, pageSize.width, pageSize.height), nil);
    CGContextTranslateCTM(pdfContext, 0, -(pageSize.height*currentPage));
    [self.view.layer renderInContext:pdfContext];
    if ((pageSize.height*(currentPage+1)) > mediaBox.size.height) done = YES;
    else currentPage++;
} while (!done);
UIGraphicsEndPDFContext();

您还可以查看此工作项目ScrollViewToPDF。 它使用相同的滚动视图的图层 renderInContext 但这里的 PDF 是根据您的要求创建的,例如一页 PDF 或多页 PDF。 它捕获了 scrollView 的所有可见和不可见部分

【讨论】:

    【解决方案2】:

    将某些内容渲染到上下文中只会渲染当前视图层次结构中的内容。这意味着,如果您使用UITableViewUICollectionView,则在任何给定时间,并不是每个代表您的数据的单元格都在层次结构中。如果是我,我会尝试暂时将视图设置为具有巨大的框架,以便所有内容都在层次结构中。如果这不起作用,我将编写一个自定义视图,根据请求可以为所有数据布局所有内容,然后在渲染完成后重置为更有效的布局。

    【讨论】:

      【解决方案3】:

      尝试以下代码将UIView 转换为PDF。

      请注意,以下方法仅创建视图的位图;它不会创建实际的排版。

      -(void)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);
      }
      

      还要确保你导入:QuartzCore/QuartzCore.h

      【讨论】:

        猜你喜欢
        • 2016-02-03
        • 2013-08-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多