如何将复杂的UIView渲染成具有高分辨率的PDF上下文?

Dav*_*d H 4 pdf-generation uiview ios

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

Dav*_*d H 9

最后,我能够从几个先前的帖子中获取提示并组合一个解决方案.我发布这个因为我花了很长时间才开始工作,我真的希望能节省别人的时间和精力.

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

  1. 将UIView渲染为缩放的位图上下文以生成大图像
  2. 将图像绘制为缩小的PDF上下文,以便绘制的图像具有高分辨率

建立你的观点:

let v = UIView()
... // then add subviews, constraints, etc
Run Code Online (Sandbox Code Playgroud)

创建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
Run Code Online (Sandbox Code Playgroud)

然后使用扩展比例创建图像的大位图:

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()
Run Code Online (Sandbox Code Playgroud)

现在我们有一个大图像,每个点代表一个像素.为了以高分辨率将其绘制到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
Run Code Online (Sandbox Code Playgroud)

您可以看到,与绘制的"S"相比,奶油色位图中包含的左"S"看起来相当不错,但是属性字符串:

在此输入图像描述

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

在此输入图像描述