Chart snapshot in sChartRenderFinished


#1

Hi,

I try to get a snapshot of my charts under iOS7, I naturally do it in sChartRenderFinished

It is properly called, but snapshot I get is an empty image (of right size). Here is the code

Thanks for any help

Sylvain

CGContextRef context = UIGraphicsGetCurrentContext();
        if ([_chart respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)]) {
                [_chart drawViewHierarchyInRect:_chart.bounds afterScreenUpdates:YES];
            } else {
                [_chart.layer renderInContext:context];
            }
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        NSLog(@"Size height %f width %f", image.size.height, image.size.width); // we get proper image size
        UIGraphicsEndImageContext();
        
        NSData *imageData = UIImagePNGRepresentation(image); //convert image into .png format.
        
        NSFileManager *fileManager = [NSFileManager defaultManager];//create instance of NSFileManager
        
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //create an array and store result of our search for the documents directory in it
        
        NSString *documentsDirectory = [paths objectAtIndex:0]; //create NSString object, that holds our exact path to the documents directory
        
        NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png", @"chart"]]; //add our image to the path
        
        [fileManager createFileAtPath:fullPath contents:imageData attributes:nil]; //finally save the path (image)
        
        NSLog(@"image saved"); // image is empty

#2

Any news about this issue ?


#3

Hey NeoSylvain,

Our chart’s are rendered in OpenGL, so taking a snapshot isn’t that simple unfortunately! We have a blog post that covers how to do this, here’s the link.

Thanks,

Jan Akerman


#4

Thanks, I get the graph, I just have issues with the annotations which are cut (both in width and height)


#5

Hi NeoSylvain,

We’ve updated the blog post to talk about iOS7 methods of snapshotting: http://www.shinobicontrols.com/blog/posts/2014/02/24/taking-a-chart-snapshot-in-ios7

Does this fix the issues with the annotations?


#6

No it doesn’t

It seems the snapshot doesn’t take into account those changes:

CGRect f = annotationMoyenne.label.frame;
f.origin.x = f.origin.x + f.size.width/2.f;
f.origin.y = f.origin.y - f.size.height/2.f;
annotationMoyenne.label.frame = f;