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!

1 Answer 1

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!!!

Sign up to request clarification or add additional context in comments.

3 Comments

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"?
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?
Ah, got it! Makes sense. Thanks I got this all working now!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.