【问题标题】:ios- pdf generation in uiscrollviewios- uiscrollview 中的 pdf 生成
【发布时间】:2012-09-22 07:27:40
【问题描述】:

在我的 ios 应用程序中,我在滚动视图中列出了一些数据。单击按钮时,页面中的数据将以 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);
}

从上面的代码,我能够生成一个 pdf 文件,但我面临以下问题。 如果我有超过 50 个数据,在我的应用程序中,我只能看到前 10 个数据,并看到我需要滚动它们的其他数据。在这种情况下,当生成 pdf 文件时,它只获取前 10 个数据,而不是其他数据。如何也获取其他数据

【问题讨论】:

    标签: objective-c ios pdf-generation


    【解决方案1】:

    renderInContext: 只会呈现您当前看到的内容 - UIScrollView 很智能,并且只会在层次结构中拥有您现在需要的视图。您需要一个 for 循环来手动滚动和重绘。这可能很难正确处理,可能在滚动后手动调用 [aView layoutSubviews] 以强制 scrollView 立即重新排列子视图。

    【讨论】:

    • dineshprasanna:您找到正确的解决方案了吗? @steipete:你能给我们一些线索吗?我理解,但我真的不知道如何开始。
    【解决方案2】:

    正如 steipete 所说,您可以使用循环来渲染上下文 - 你需要计算循环数 numOfLoop = webview.ScrollView.ContentSize.Height/weview.frame.size.height

    • 使用循环渲染InContext for(int i = 0; i

      1.UIGraphicsBeginPDFPage()

      1. 获取当前上下文
      2. renderIncontext

    这种方式在我的应用程序中运行正确,但是当 numOfLoop lager 时我遇到了大问题 - 你会变慢 renderInContext - 内存将增加 -> 关闭应用程序 这样,当我处于一次循环中时,如何删除/释放上下文?

    其他方式,您可以使用https://github.com/iclems/iOS-htmltopdf/blob/master/NDHTMLtoPDF.h 生成PDF

    【讨论】:

      【解决方案3】:

      @steipete, @all 嗨,我尝试了下面的代码,它生成了多页 PDF,但是视图中的可见区域对于所有页面都是重复的。请添加任何进一步的想法以改进结果。

      -(void)createPDFfromUIView:(UIWebView*)aView saveToDocumentsWithFileName:(NSString*)aFilename
      
      {
      
      NSString *oldFilePath= [[NSBundle mainBundle] pathForResource:@"ABC" ofType:@"pdf"];
      
      NSMutableData *pdfData = [NSMutableData data];
      
      UIGraphicsBeginPDFContextToData(pdfData, aView.bounds, nil); 
      
      CFURLRef url = CFURLCreateWithFileSystemPath (NULL, (CFStringRef)oldFilePath, kCFURLPOSIXPathStyle, 0); 
      
      CGPDFDocumentRef templateDocument = CGPDFDocumentCreateWithURL(url); 
      
      CFRelease(url); size_t count = CGPDFDocumentGetNumberOfPages(templateDocument); 
      
      for (size_t pageNumber = 1; pageNumber <= count; pageNumber++) 
      
      { 
      
      UIGraphicsBeginPDFPage(); 
      
      CGContextRef pdfContext = UIGraphicsGetCurrentContext(); 
      
      [aView.layer renderInContext:pdfContext]; 
      
      }
      
       CGPDFDocumentRelease(templateDocument); 
      
      // 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];
      
       [pdfData writeToFile:documentDirectoryFilename atomically:YES];
      
      NSLog(@"documentDirectoryFileName: %@",documentDirectoryFilename);
      
      }
      

      【讨论】:

        【解决方案4】:

        查看ScrollViewToPDF 示例

        它使用相同的 scrollview's layer renderInContext 但这里的 PDF 是根据您的要求创建的,例如一页 PDF 或多页 PDF

        注意:它捕获了 scrollView 的所有可见和不可见部分

        【讨论】:

        • 它会创建但 PDF 会像素化
        猜你喜欢
        • 1970-01-01
        • 2013-12-30
        • 1970-01-01
        • 2012-04-22
        • 2012-05-26
        • 2013-11-26
        • 2013-08-01
        • 1970-01-01
        • 2013-02-23
        相关资源
        最近更新 更多