如何将CorePlot创建的图形保存为jpg文件
我正在使用 CorePlot 在我的应用程序中绘制不同的图表。然后,我想将此图保存为 jpg 文件并发布到某个社交网站(例如 Twitter)。
我阅读了一些文档并了解到以下方法可能对我有用:
CGBitmapContextCreateImage:从位图图形上下文(即我的图形所在的视图或整个屏幕)创建 CGImage 作为副本理解对吗?)
CGImageCreateWithImageInRect:我可以剪辑如果需要,可以剪掉图像的某些部分
[UIImage imageWithCGImage]:将 CGImage 转换为 UIImage 对象
UIImageJPEGRepresentation:将我的 UIImage 对象转换为 jpg NSData 对象,然后我可以将其分享到我的社交网络
第一个问题是:我是否正确理解了为完成任务而必须执行的操作顺序?我本来想自己尝试一下,但我遇到了一个问题,导致了第二个问题:
如果我使用 CorePlot,我从哪里获取图形上下文信息以传递到 CGBitmapContextCreateImage 中?抱歉,如果这是一个愚蠢的问题,但我不太熟悉图形上下文。
非常感谢您提前提供的任何帮助!如果我在某个地方完成了所有这些工作,我保证会在这里发布我的代码示例。
好吧,多亏了布拉德,这真的很容易,但正如我所承诺的,我必须发布以下几行:
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");
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
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:
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");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
实际上,用于 Core Plot 中所有绘图元素的 CPLayer 基类有一个名为
-imageOfLayer
的类别方法,该方法返回您在其上调用的任何图层的 UIImage 实例。如果您想了解这是如何实现的,请参阅框架中的CPPlatformSpecificCategories.h
和CPPlatformSpecificCategories.m
文件。只需在 CPGraph 实例上使用
-imageOfLayer
,然后使用UIImageJPEGRepresentation()
创建该图像的 JPEG 版本。然后您可以使用正常方式上传该图像。同样,有一个
-dataForPDFRepresentationOfLayer
方法返回一个 NSData 实例,其中包含图层及其子图层的 PDF 表示形式。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 theCPPlatformSpecificCategories.h
andCPPlatformSpecificCategories.m
files in the framework if you want to see how this is accomplished.Simply use
-imageOfLayer
on your CPGraph instance and then useUIImageJPEGRepresentation()
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.我对 CorePlot 不太了解。但我假设 CorePlot 提供了一个可以向自身呈现图形的视图。如果 CorePlot 没有提供获取图形
UIImage
的机制,那么您可能可以欺骗 CorePlot 将其绘制到您自己的位图图形上下文中,您可以从中创建要保存的图像对象。基本方法如下:
1) 使用
UIGraphicsBeginImageContext()
创建位图图形上下文。这也将使上下文成为“当前”上下文。您还可以使用CGBitmapContextCreate()
,但随后您还必须使用UIGraphicsPushContext()
使其成为当前状态。2) 调用CorePlot视图的
drawRect:
方法。3) 使用
UIGraphicsGetImageFromCurrentImageContext()
从图形上下文中获取图像4) 使用
UIGraphicsEndImageContext()
进行清理5) 将 Jpeg 位放入
NSData
> 使用UIImageJPEGRepresentation()
对象。6)使用
NSData
方法(或其他..)保存/上传你的jpegI 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 useCGBitmapContextCreate()
but then you'd also have to useUIGraphicsPushContext()
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 usingUIImageJPEGRepresentation()
.6) save/upload your jpeg using
NSData
methods (or other..)