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

后端 未结 2 1294
我在风中等你
我在风中等你 2021-02-02 01:22

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

2条回答
  •  佛祖请我去吃肉
    2021-02-02 01:49

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

提交回复
热议问题