在屏幕外渲染uiview以捕获为uiimage



在我正在编写的一个个人swift应用程序中,我使用一个图表库来创建条形图,并将其显示在视图控制器中。到目前为止一切都很好。现在我想创建其他图表,将它们捕获为uiimage以包含在PDF表单中而不显示图表。图表库创建了一个uiview,我尝试了许多建议。post1,还有这一个post2,但运气不好。。。

该代码有效,但迫使我显示图表

resourceBarChart.updateDataEntries(dataEntries: resChartData, animated: false)
self.view.addSubview(resourceBarChart)
let myimage1 = self.view.subviews[1].image(withScale: .native)

省略uiview 的显示

resourceBarChart.updateDataEntries(dataEntries: resChartData, animated: false)
let myimage = resourceBarChart.image(withScale: .native)

返回一个空画布。我试着玩隐藏的财产,但运气不好(也许正如预期的那样(。作为这方面的新手,我怀疑我忽略了一个微妙之处,并对的任何建议持开放态度

在将视图捕获为UIImage 之前,请尝试使用drawHierarchy

resourceBarChart.drawHierarchy(in: resourceBarChart.bounds, afterScreenUpdates: true)

最新更新