我正在使用CorePlot在我的应用程序中绘制不同的图形。然后,我想将此图保存为jpg文件并发布到某个社交网站(例如Twitter)。如何将由CorePlot创建的图形保存到jpg文件
我做了一些文档的阅读和理解,下面的方法可以为我是有用的:
CGBitmapContextCreateImage:创建一个CGImage从位图图形上下文的副本(即认为在我的图是情境或整个屏幕 - 有我理解右)
CGImageCreateWithImageInRect:如果需要,我可以剪辑出的图像的一些部分
[UIImage的imageWithCGImage? ]:转换CGImage到UIImage对象
UIImageJPEGRepresentation:我UIImage对象转换为JPG NSData对象,然后我可以分享到我的社交网络
的第一个问题是:有我的理解正确的为了完成我的任务,我必须执行哪些操作?我会尝试过了自己,但我有这导致了第二个问题一个问题:
从哪儿可以得到图形上下文的信息传递到CGBitmapContextCreateImage如果我使用CorePlot?对不起,如果这是一个愚蠢的问题,但我不是真的在家里与图形上下文。
非常感谢提前任何帮助!如果我能在这里找到所有这些,我保证会在这里发布我的代码示例。
好,多亏了布拉德,这是很容易,但我promissed,我必须张贴线路:
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");
作为一个附注,你认为PNG而不是JPG?计算机生成的图形如图表可能会更好。 – ustun 2010-11-19 18:15:55