如何将CorePlot创建的图形保存为jpg文件 [英] How to save a graphic created by CorePlot to a jpg file

查看:168
本文介绍了如何将CorePlot创建的图形保存为jpg文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用CorePlot在我的应用程序中绘制不同的图形。然后,我想将此图保存到jpg文件并发布到某个社交网站(例如Twitter)。

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:从位图图形上下文创建CGImage作为副本(即,查看我的图表所在的位置或整个屏幕 - 我理解对了吗?)

  • 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:如果需要,我可以剪掉图像的某些部分

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

[UIImage imageWithCGImage]:将CGImage转换为UIImage对象

[UIImage imageWithCGImage]: converting CGImage to an UIImage object

UIImageJPEGRepresentation:转换我的UIImage反对jpg NSData对象然后我可以分享到我的社交网络

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


  1. 第一个问题是:我理解对了吗我必须执行的操作顺序才能完成我的任务?我会自己尝试一下,但是我遇到了一个问题导致了第二个问题:

  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:

从哪里获取图形上下文信息到如果我正在使用CorePlot,则进入 CGBitmapContextCreateImage ?对不起,如果这是一个愚蠢的问题,但我不是真的在家里有图形上下文。

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.

好吧,多亏了Brad,这真的很容易,但正如我所承诺的,我有发布行:

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


推荐答案

实际上,CPLayer基类用于所有绘图元素在Core Plot中有一个名为 -imageOfLayer 的类别方法,它返回你调用它的任何层的UIImage实例。如果您想了解如何完成此操作,请参阅框架中的 CPPlatformSpecificCategories.h CPPlatformSpecificCategories.m 文件。

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.

只需在CPGraph实例上使用 -imageOfLayer ,然后使用 UIImageJPEGRepresentation()创建该图像的JPEG版本。然后,您可以使用常规方法上传该图像。

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.

同样,还有一个 -dataForPDFRepresentationOfLayer 方法,它返回一个NSData实例,其中包含该层的PDF表示及其子层。

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

这篇关于如何将CorePlot创建的图形保存为jpg文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆