好的,这就是我所做的
选项 1:使用 UIView.layer 在 PDF CGContect 上呈现
func drawLineGraph(x: CGFloat, y: CGFloat)->CGRect{
let width = (pageSize.width - 2*kBorderInset - 2*kMarginInset)/2.0 - 50.0
let renderingRect = CGRect(x: x, y: y + 50.0, width: width, height: 150.0)
// Create a view for the Graph
let graphController = LineChartController(rect: renderingRect, building: self.building)
if let currentContext = UIGraphicsGetCurrentContext() {
let frame = graphController.chartView.frame
currentContext.saveGState()
currentContext.translateBy(x:frame.origin.x, y:frame.origin.y);
graphController.chartView.layer.render(in: currentContext)
currentContext.restoreGState()
}
return renderingRect
}
graphController 只是一个对象,它与包含图形的通常父 ViewController 具有基本相同的功能。设置图形参数和数据。
完成后,调用下面的函数以在 PDF 页面上下文中呈现。
将图表放置在正确的位置需要一点翻译。
选项 2:在 PDF 页面上绘图 CGContect
而解决方案是...ta da...
func drawBarGraph(x: CGFloat, y: CGFloat)->CGRect{
let width = (pageSize.width - 2*kBorderInset - 2*kMarginInset)/2.0 - 50.0
let renderingRect = CGRect(x: x + width + 50, y: y + 50.0, width: width, height: 150.0)
// Create a view for the Graph
let graphController = BarChartController(rect: renderingRect, building: self.building)
if let currentContext = UIGraphicsGetCurrentContext() {
let frame = graphController.chartView.frame
currentContext.saveGState()
currentContext.translateBy(x:frame.origin.x, y:frame.origin.y)
//graphController.chartView.layer.render(in: currentContext)
graphController.chartView.draw(frame)
currentContext.restoreGState()
}
return renderingRect
}
由于当前上下文设置为 PDF 页面的上下文,只需调用图表 draw() 函数直接传递框架矩形。
我错过了什么,能这么简单吗?
您可以找到generated PDF here as well as sample code 的副本。