Chart screenshot produces empty image


I have successfully called the method:

-(UIImage *)chartImage {
    // create an image context - the size of chart and the scale of the device screen
    UIGraphicsBeginImageContextWithOptions(_chart.bounds.size, NO, 0.0);
        // render our snapshot into the image context
        [_chart drawViewHierarchyInRect:_chart.bounds afterScreenUpdates:NO];
        // grab the image from the context
        UIImage *chartImage = UIGraphicsGetImageFromCurrentImageContext();
    // Finish using the context
    return chartImage;

This works if the chart is visible on the screen, howver if I create the chart but do not add it as a subview to display on screen then the above code produces the right sized image but it is blank?

I have a need to cycle through many charts and create a snapshot of each ready to export so it is not practical to display them momentarly in order to grab the image…

Should this method still work even if the chart is not visible but has been created?


Have since found that drawViewHierarchyInRect only works on what is visible on screen…DOH!

Is there a method for IOS7 that grabs the chart content regardless of on screen or not?



** bump ** 




I don’t think you can screenshot something that isn’t added to a view heirachy, but you can screenshot something that is placed off screen. You can do this using the iOS7 method snapshotViewAfterScreenUpdates:, for example:

UIView *snapshotView = [_chart snapshotViewAfterScreenUpdates:NO];

You’ll get a UIView back, which you can easily create a UIImage from.



Hi, but from what I understand of that method that has to be visible on the screen too?  from the docs: 

If the current view is not yet rendered, perhaps because it is not yet onscreen, the snapshot view has no visible content.


UIView *snapshotView = [graphView.chart snapshotViewAfterScreenUpdates:NO];

results in snapshotView being nil


UIView *snapshotView = [graphView.chart snapshotViewAfterScreenUpdates:YES];

results in an image of the right size but blank

???  :confused:


I am getting partial success by doing the following:

// add the chart view to tempView that is positioned off screen
[tempView addSubview:chartView];
UIGraphicsBeginImageContextWithOptions(chartView.bounds.size, YES, 0.0);
[chartView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();

And I get a chart output with gridlines, x,y labels and chart labels…but the seires lines are missing???

See attached image 

I have a real need to cycle all charts in my app to export the chart as an image, but if for exapmple I have 8 charts then I create each in turn off screen, export, remove and move onto next…



This works well for me. I’m in the process of doing one for Android now.

var imageSize = new SizeF (612, 612);
var imageRect = new RectangleF (0, 0, 612, 612);

theChart = ShinobiChart(imageRect);
// Here insert where you set up the chart with data, axes, etc
theChart.RedrawChart ();

UIGraphics.BeginImageContextWithOptions(imageSize, false, 0f);


// Create a view and add chart and any decorationw you want to it

UIImage theFlightGraph = UIGraphics.GetImageFromCurrentImageContext (); 
NSData graphImageData = theFlightGraph.AsPNG ();

UIGraphics.EndImageContext ();