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

亡梦爱人 提交于 2019-12-02 19:32:09

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.

TomSwift

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!