【问题标题】:How to render a complex UIView into a PDF Context with high resolution?如何将复杂的 UIView 渲染成高分辨率的 PDF 上下文?
【发布时间】:2016-05-28 07:12:36
【问题描述】:

关于 SO 有几个问题询问如何将 UIView 渲染到 PDF 上下文中,但它们都使用 view.layer.renderInContext(pdfContext),这会产生 72 DPI 图像(打印时看起来很糟糕)。我正在寻找的是一种以某种方式让 UIView 以 300 DPI 呈现的技术。

【问题讨论】:

    标签: ios uiview pdf-generation


    【解决方案1】:

    最后,我能够从之前的几篇文章中获得提示,并提出了一个解决方案。我发布这篇文章是因为我花了很长时间才开始工作,我真的希望能节省其他人的时间和精力。

    此解决方案使用两种基本技术:

    1. 将 UIView 渲染到缩放的位图上下文中以生成大图像
    2. 将图像绘制到已按比例缩小的PDF Context中,使绘制的图像具有高分辨率

    构建你的视图:

    let v = UIView()
    ... // then add subviews, constraints, etc
    

    创建 PDF 上下文:

    UIGraphicsBeginPDFContextToData(data, docRect, stats.headerDict) // zero == (612 by 792 points)
    
    defer { UIGraphicsEndPDFContext() }
    
    UIGraphicsBeginPDFPage();
    
    guard let pdfContext = UIGraphicsGetCurrentContext() else { return nil }
    
    // I tried 300.0/72.0 but was not happy with the results
    let rescale: CGFloat = 4 // 288 DPI rendering of VIew
    
    // You need to change the scale factor on all subviews, not just the top view!
    // This is a vital step, and there may be other types of views that need to be excluded
    

    然后创建一个放大的图像的大位图:

    func scaler(v: UIView) {
       if !v.isKindOfClass(UIStackView.self) {
          v.contentScaleFactor = 8
       }
       for sv in v.subviews {
          scaler(sv)
       }
    }
    scaler(v)
    
    // Create a large Image by rendering the scaled view
    let bigSize = CGSize(width: v.frame.size.width*rescale, height: v.frame.size.height*rescale)
    UIGraphicsBeginImageContextWithOptions(bigSize, true, 1)
    let context = UIGraphicsGetCurrentContext()!
    
    CGContextSetFillColorWithColor(context, UIColor.whiteColor().CGColor)
    CGContextFillRect(context, CGRect(origin: CGPoint(x: 0, y: 0), size: bigSize))
    
    // Must increase the transform scale
    CGContextScaleCTM(context, rescale, rescale)
    
    v.layer.renderInContext(context)
    
    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    

    现在我们有一个大图像,每个点代表一个像素。 为了将其以高分辨率绘制到 PDF 中,我们需要在以大尺寸绘制图像的同时缩小 PDF:

    CGContextSaveGState(pdfContext)
    CGContextTranslateCTM(pdfContext, v.frame.origin.x, v.frame.origin.y) // where the view should be shown
    
    CGContextScaleCTM(pdfContext, 1/rescale, 1/rescale)
    
    let frame = CGRect(origin: CGPoint(x: 0, y: 0), size: bigSize)
    image.drawInRect(frame)
    
    CGContextRestoreGState(pdfContext)
    
    ... // Continue with adding other items
    

    您可以看到奶油色位图中左侧的“S”与绘制的“S”相比看起来相当不错,但它是一个属性字符串:

    当通过简单的 PDF 渲染而不进行所有缩放来查看相同的 PDF 时,您会看到以下内容:

    【讨论】:

    • 嗨,David H,有没有办法让您的代码适应 uiwebview ?我不仅尝试以合理的质量呈现 pdf,而且还在其上添加图像并将其保存到磁盘(类似于在任何我想要的地方签署 pdf)
    • @H4Hugo 我之前错过了这条评论——抱歉耽搁了。不,我不知道,但如果您找到方法,请在此处发布。我想这样做的方法是创建一个比您要捕获的视图大 4 倍的 Web 视图 - 但它只会包含更多内容。如果视图是视网膜,您可以获得 2 倍图像。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-23
    • 1970-01-01
    • 2021-08-08
    • 1970-01-01
    相关资源
    最近更新 更多