我正在使用CorePlot在我的应用程序中绘制不同的图形.然后,我想将此图保存到jpg文件并发布到某个社交网站(例如,Twitter).
我做了一些文档阅读,并了解以下方法对我有用:
CGBitmapContextCreateImage:从位图图形上下文(即我的图形所在的视图或整个屏幕)创建CGImage作为副本 - 我理解对了吗?)
CGImageCreateWithImageInRect:如果需要,我可以剪掉图像的某些部分
[UIImage imageWithCGImage]:将CGImage转换为UIImage对象
UIImageJPEGRepresentation:将我的UIImage对象转换为jpg NSData对象,然后我可以将其分享到我的社交网络
第一个问题是:我是否理解了为完成任务我必须执行的操作顺序?我会自己尝试一下,但是我遇到了一个导致第二个问题的问题:
从哪儿可以得到图形上下文的信息传递到CGBitmapContextCreateImage如果我使用CorePlot?对不起,如果这是一个愚蠢的问题,但我不是真的在家里有图形上下文.
非常感谢您提前帮助!如果我在这里得到所有这些,我保证在这里发布我的代码示例.
好吧,多亏了布拉德,这真的很容易,但正如我所承认的那样,我必须发布这些内容:
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");
Run Code Online (Sandbox Code Playgroud)
Bra*_*son 20
实际上,用于Core Plot中所有绘图元素的CPLayer基类都有一个名为的类别方法-imageOfLayer,它返回你调用它的任何层的UIImage实例.如果要查看如何完成此操作,请参阅框架中的CPPlatformSpecificCategories.h和CPPlatformSpecificCategories.m文件.
只需-imageOfLayer在CPGraph实例上使用,然后使用它UIImageJPEGRepresentation()来创建该图像的JPEG版本.然后,您可以使用常规方法上传该图像.
同样,有一个-dataForPDFRepresentationOfLayer方法返回一个NSData实例,该实例包含该层及其子层的PDF表示.
| 归档时间: |
|
| 查看次数: |
3021 次 |
| 最近记录: |