2

My chart shows up fine in my app, but when I save the chartView to camera roll or for use in another UIViewController the bars don't show up.

Fine in my app:

enter image description here

Bars don't show up when save the chartView to camera roll or for use in another UIViewController:

enter image description here

I've tried a few ways of doing this -specifically for saving the chartView to Camera Roll- like:

1)

    let image1 = chartView.getChartImage(transparent: false)
    UIImageWriteToSavedPhotosAlbum(image1!, nil, nil, nil)

enter image description here

2)

    let image2 = captureScreen()
    UIImageWriteToSavedPhotosAlbum(image2!, nil, nil, nil)

enter image description here

3)

extension UIImage {
    convenience init(view: UIView) {
        UIGraphicsBeginImageContext(view.frame.size)
        view.layer.render(in: UIGraphicsGetCurrentContext()!)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        self.init(cgImage: (image?.cgImage)!)
    }
}

    let image3 = UIImage(view: chartView)
    UIImageWriteToSavedPhotosAlbum(image3, nil, nil, nil)

enter image description here

But nothing works for me. Any ideas why? Thanks!

SRMR
  • 3,064
  • 6
  • 31
  • 59

1 Answers1

1

I had the same issue when capturing image using getChartImage. So I made my own function for capturing Image.

Basically I make a UIView class and inherited LineChartView. Made UIView extension for taking snapShot of my chartView.

Implement this function in UIView extension:

func snapShot() -> UIImage? {
        UIGraphicsBeginImageContextWithOptions(self.frame.size , false, UIScreen.main.scale)
        self.layer.render(in: UIGraphicsGetCurrentContext()!)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        return image
    }

And call it like this

let image = chartView.snapShot()

Hope this helps!!!

Zohaib
  • 2,845
  • 2
  • 23
  • 33
  • Thanks for the response! Glad to know someone else was having trouble with the same thing and figured it out. I think I understand that you put the `snapshot()` function inside of `extension UIView {...}`, but wondering what is in the UIView class and inherited LineChartView"? – SRMR Jul 04 '17 at 14:28
  • I did not make ViewController and inherit LineChartView. rather then I made a class and inherited LineChartView. So the chart is a view for me. so thats why I made UIView extension. Is it clear? – Zohaib Jul 04 '17 at 16:37
  • Ah, got it! Makes sense. Thanks I got this all working now! – SRMR Jul 04 '17 at 16:56