Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save a graphic created by CorePlot to a jpg file

I am using CorePlot to draw different graphs in my application. Then, I would like to save this graph to a jpg file and publish to some social networking site (e.g., Twitter).

I did some documentation reading and understood that the following methods could be useful for me:

  • CGBitmapContextCreateImage: creating a CGImage as a copy from a bitmap graphics context (i.e., the view where my graph is situated or the whole screen - have I understood right?)

  • CGImageCreateWithImageInRect: I can clip out some part of the image if needed

  • [UIImage imageWithCGImage]: converting CGImage to an UIImage object

  • UIImageJPEGRepresentation: converting my UIImage object to a jpg NSData object which then I can share to my social network

    1. The first question is: have I understood right the sequence of operations I have to carry out to accomplish my task? I would have tried it out myself, but I've got a problem which leads to the second question:

    2. From where do I get the graphics context info to pass into CGBitmapContextCreateImage if I am using CorePlot? Sorry if it's a dumb question, but I am not really at home with graphics contexts.

Thanks a lot for any help in advance! And if I get with all this somewhere I promise to post my code sample here.

Well, thanks to Brad, it was really easy, but as I promissed, I have to post the lines:

CPXYGraph *graph=[[CPXYGraph alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];

// setting up the graph
// ...

UIImage *newImage=[graph imageOfLayer];
NSData *newPNG=UIImagePNGRepresentation(newImage); // or you can use JPG or PDF

// For test purposes I write the file to the Documents directory, but you can do whatever you want with your new .png file
NSString *filePath=[NSString stringWithFormat:@"%@/graph.png", [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]];

if([newPNG writeToFile:filePath atomically:YES]) 
    NSLog(@"Created new file successfully");
like image 685
Arts Avatar asked Nov 19 '10 16:11

Arts


2 Answers

Actually, the CPLayer base class used for all drawing elements in Core Plot has a category method called -imageOfLayer that returns a UIImage instance of whatever layer you call it on. See the CPPlatformSpecificCategories.h and CPPlatformSpecificCategories.m files in the framework if you want to see how this is accomplished.

Simply use -imageOfLayer on your CPGraph instance and then use UIImageJPEGRepresentation() to create the JPEG version of that image. You can then upload that image using the normal means.

Likewise, there is a -dataForPDFRepresentationOfLayer method that returns an NSData instance containing a PDF representation of the layer and its sublayers.

like image 149
Brad Larson Avatar answered Nov 07 '22 17:11

Brad Larson


I dont know much about CorePlot. But I assume CorePlot provides a view that renders a graph to itself. If CorePlot doesn't provide a mechanism to get a UIImage of the graph, then you can probably trick CorePlot into drawing into your own Bitmap Graphics Context, from which you can create an image object to save.

Here's the basic approach:

1) create a bitmap graphics context using UIGraphicsBeginImageContext(). This will also make the context the "current" context. You could also use CGBitmapContextCreate() but then you'd also have to use UIGraphicsPushContext() to make it current.

2) call the CorePlot view's drawRect: method.

3) get the image from the graphics context using UIGraphicsGetImageFromCurrentImageContext()

4) clean up with UIGraphicsEndImageContext()

5) get your Jpeg bits into a NSData object using UIImageJPEGRepresentation().

6) save/upload your jpeg using NSData methods (or other..)

like image 2
TomSwift Avatar answered Nov 07 '22 15:11

TomSwift