Skip to main content

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");


Source: Tips4allCCNA FINAL EXAM

Comments

  1. 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.

    ReplyDelete
  2. 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 using UIImageJPEGRepresentation().

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

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.