0

in a personal swift app i am writing, i am using a charting library to create bar charts and display them in a viewcontroller. everything is working fine so far. Now i would like to create other charts, capture them as a uiimage to include in a PDF form ... without displaying the charts. The charting library creates a uiview and I tried many of the suggestions. post1, and this one post2 but without much luck...

this code works but forces me to display the chart

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

omitting the display of the uiview

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

returns an empty canvas. i tried playing with the hidden property but without luck (maybe as expected). Being new at this, i suspect there is a subtlety that i am overlooking and open for any suggestions

Sami
  • 67
  • 7

1 Answers1

0

Try use drawHierarcy before capture the view as UIImage

resourceBarChart.drawHierarchy(in: resourceBarChart.bounds, afterScreenUpdates: true)
aiwiguna
  • 2,852
  • 2
  • 15
  • 26
  • Thank you very much ... it worked ! although i am getting this message `[Snapshotting] View (0x7fc7a0d398d0, myApp.BasicBarChart) drawing with afterScreenUpdates:YES inside CoreAnimation commit is not supported.` – Sami Jul 15 '20 at 23:35
  • i am not very sure about that message, maybe you can change where you call the drawHierarchy function – aiwiguna Jul 16 '20 at 03:34