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

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex