【问题标题】:Creating PDF from UIScrollView in iphone app在 iPhone 应用程序中从 UIScrollView 创建 PDF
【发布时间】:2013-05-12 04:17:16
【问题描述】:

我正在创建从 UIView 到 pdf 的 pdf,它工作正常,但我有 scrollView 的内容,我想将其转换为 pdf,但它只显示 pdf 中的可见部分,而不是整个 scrollView 内容。这是我的代码

-(void)createPDFfromUIView:(UIView*)aView saveToDocumentsWithFileName:(NSString*)aFilename
 {    
    NSMutableData *pdfData = [NSMutableData data];

    // Get Scrollview size
    CGRect scrollSize = CGRectMake(1018,76,scrollView.contentSize.width,scrollView.contentSize.height);

    // Points the pdf converter to the mutable data object and to the UIView to be converted
    UIGraphicsBeginPDFContextToData(pdfData, scrollSize, 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);
}

【问题讨论】:

  • 访问这个答案,它会帮助你。 stackoverflow.com/a/6566696/1223897
  • @Yuvrajsinh 这是我正在使用的相同代码,但它是普通的而不是 UIScrollView
  • 我认为问题在于,您正在创建具有静态大小的新 UIView。使用 yourScrollView.contentSize 在滚动视图中获取准确的内容大小(宽度、高度),无需创建新视图。
  • 你能在我的代码中编辑如何做到这一点
  • 你最后得到正确答案了吗?

标签: iphone ios ipad uiscrollview


【解决方案1】:

这为您提供了 UIScrollView 的可见部分

CGRect visibleRect;
visibleRect.origin = scrollView.contentOffset;
visibleRect.size = scrollView.bounds.size;

float theScale = 1.0 / scale;
visibleRect.origin.x *= theScale;
visibleRect.origin.y *= theScale;
visibleRect.size.width *= theScale;
visibleRect.size.height *= theScale;

所以你可以在UIGraphicsBeginPDFContextToData(pdfData, aView.bounds, nil);中使用它
通过参数的一些细微变化。

【讨论】:

  • 这段代码在哪里写,请您在我的代码中编辑
  • 你能帮帮我的比例是多少
  • scale 应该是 UIScrollView 的宽度
  • 同样的问题,它只是显示可见区域而不是整个视图
  • 你想要 ScrolVIew ryt 的全部数据..?
【解决方案2】:
(void)createPDFfromUIView:(UIScrollView*)aView saveToDocumentsWithFileName:(NSString*)aFilename
{
    // Creates a mutable data object for updating with binary data, like a byte array
    NSMutableData *pdfData = [NSMutableData data];

    // Get Scrollview size
    CGRect scrollSize = CGRectMake(aView.origin.x,aView.origin.y,aView.contentSize.width,aView.contentSize.height);

    // Points the pdf converter to the mutable data object and to the UIView to be converted
    UIGraphicsBeginPDFContextToData(pdfData, scrollSize, 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);
}

【讨论】:

  • aView.originalx 我们要给出scrollview的原位置
  • 这些答案有棘手的解决方案,请参考:stackoverflow.com/a/5405944/1223897stackoverflow.com/a/14292471/1223897
  • 我已经检查了这两个,但我不清楚这些
  • 检查我的更新代码,它也显示滚动条,但没有显示任何内容,这意味着它正在获取 scrollView 大小但没有获取内容
【解决方案3】:

我用来从 uiscrollview 制作 pdf 的这段代码确实有帮助,但它不会像我们在 pdf 上绘制那样好 -- 请看 -- 来自 UIScrollView 的 PDF

- (void) createPDF
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *directroyPath = nil;
    directroyPath = [documentsDirectory stringByAppendingPathComponent:@"temp"];
    NSString *filePath = [directroyPath stringByAppendingPathComponent:@"test.pdf"];
    // check for the "PDF" directory
    NSError *error;
    if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {

    } else {
        [[NSFileManager defaultManager] createDirectoryAtPath:directroyPath
                                  withIntermediateDirectories:NO
                                                   attributes:nil
                                                        error:&error];
    }

    CGContextRef pdfContext = [self createPDFContext:_scrollView2.bounds path:(CFStringRef)filePath];
    NSLog(@"PDF Context created");

    /*
     Here limit of i is no of pages in your uiscrollview or
     you can use hit and trial here because there is a
     length of page is going to be equal to 
     the visible size of uiscrollView in your application 
   */

    for (int i = 0 ; i< 2 ; i++)
    {

        // page 1
        CGContextBeginPage (pdfContext,nil);

        //turn PDF upsidedown
        CGAffineTransform transform = CGAffineTransformIdentity;

       //here 365 is equal to the height of myScrollView.frame.size.height

        transform = CGAffineTransformMakeTranslation(0, (i+1) * 365);
        transform = CGAffineTransformScale(transform, 1.0, -1.0);
        CGContextConcatCTM(pdfContext, transform);

        //Draw view into PDF
        [_scrollView2.layer renderInContext:pdfContext];
        CGContextEndPage (pdfContext);
        [_scrollView2 setContentOffset:CGPointMake(0, (i+1) * 365) animated:NO];

    }
    CGContextRelease (pdfContext);
}

- (CGContextRef) createPDFContext:(CGRect)inMediaBox path:(CFStringRef) path
{
    CGContextRef myOutContext = NULL;
    CFURLRef url;
    url = CFURLCreateWithFileSystemPath (NULL, path,
                                         kCFURLPOSIXPathStyle,
                                         false);

    if (url != NULL) {
        myOutContext = CGPDFContextCreateWithURL (url,
                                                  &inMediaBox,
                                                  NULL);
        CFRelease(url);
    }
    return myOutContext;
}

【讨论】:

    【解决方案4】:

    将滚动视图内容转换为 PDF 文件的最简单方法是将内容视图作为滚动视图的第一个子视图,并使用该视图拍摄滚动视图的快照。 Swift 中的代码如下所示:

    func generatePDFdata(withView view: UIView) -> NSData {
    
      let pageDimensions = view.bounds
      let outputData = NSMutableData()
    
      UIGraphicsBeginPDFContextToData(outputData, pageDimensions, nil)
      if let context = UIGraphicsGetCurrentContext() {
          UIGraphicsBeginPDFPage()
          view.layer.render(in: context)
      }
      UIGraphicsEndPDFContext()
    
      return outputData
    }
    

    然后您可以使用outputData 写入 PDF 文件:

    outputData.write(to: pdfFileUrl, atomically: true)
    

    【讨论】:

      猜你喜欢
      • 2013-05-12
      • 2013-11-23
      • 1970-01-01
      • 2018-05-16
      • 1970-01-01
      • 1970-01-01
      • 2015-10-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多