【问题标题】:Saving the whole uiview in PDF将整个 uiview 保存为 PDF
【发布时间】:2012-04-13 18:22:31
【问题描述】:

我知道如何将 uiview 保存为 PDF,但我的问题是如何将整个 uiview 保存为 PDF,例如,如果您的页面比 iPad 显示长并且您需要滚动它,我只能保存PDF 文件中的活动视图,但其余视图不存在。 我正在寻求帮助或任何对我有帮助的教程。

提前致谢。

【问题讨论】:

    标签: objective-c xcode user-interface uiviewcontroller xcode4.2


    【解决方案1】:

    您可以像这样将视图呈现为 PDF:

    #define kDefaultPageHeight 1024
    #define kDefaultPageWidth  768
    #define kMargin 5
    
    CGRect origframe = someView.frame;
    NSString *heightStr = origFrame.size.height;
    int height = [heightStr intValue];
    
    // Size of the view in the pdf page
    CGFloat maxHeight   = kDefaultPageHeight - 2*kMargin;
    CGFloat maxWidth    = kDefaultPageWidth - 2*kMargin;
    int pages = ceil(height / maxHeight); // or use 1 if you only have 1 page.
    
    UIGraphicsBeginPDFContextToFile(@"some/full/path.pdf", CGRectZero, nil);
    UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, kDefaultPageWidth, kDefaultPageHeight), nil);
    CGContextRef currentContext = UIGraphicsGetCurrentContext();
    CGContextTranslateCTM(currentContext, kMargin, kMargin);
    
    /* this is the view that you will render to the PDF */
    [someView.layer renderInContext:currentContext];
    
    UIGraphicsEndPDFContext();
    

    当然这是一个粗略的 sn-p 代码,您需要根据自己的需要进行调整

    【讨论】:

    • 为了完整起见,虽然renderInContext 是最佳答案,但如果您使用CALayer 的某些功能,它可能无法正常工作。见Can't capture masks within view layer
    • 让我试试,我们会看到,但请告诉我,这些代码是否可以保存较长的页面?
    • 还有一件事,在这段代码中,您使用 CGRect 来绘制框架并查看如果使用情节提要会发生什么?如何实施?以及如何将其他视图作为其他页面添加到此 pdf 中。
    • 如何将多个视图转换为一个包含多个页面的 PDF 文件? PLEEEAASSEEE 我很无奈!!
    • 我刚刚发现我必须进行硬编码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-22
    • 2015-08-11
    • 2017-01-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多