如何在iphone应用程序中将饼图保存为pdf文件
Posted
技术标签:
【中文标题】如何在iphone应用程序中将饼图保存为pdf文件【英文标题】:how to save a pie graph in pdf file in iphone app 【发布时间】:2012-04-20 09:02:01 【问题描述】:我有创建饼图的 iphone 应用程序,我希望该图表应保存在 iphone 的 pdf 文件中。
下面是 PieChart 的代码,但我如何将其保存为 pdf
-(void)createGraph
PieClass *myPieClass=[[PieClass alloc]initWithFrame:CGRectMake(400,40, 320, 230)];
myPieClass.itemArray=[[NSArray alloc]initWithObjects:textFieldOne.text,textFieldTwo.text,textFieldThree.text, nil];
myPieClass.myColorArray=[[NSArray alloc]initWithObjects:[UIColor purpleColor],[UIColor redColor],[UIColor orangeColor], nil];
myPieClass.radius=100;
[self.view addSubview:myPieClass];
[self creatPDFFromView:@"mydata.pdf"];
-(void)createPDFfromUIView:(UIView*)aView saveToDocumentsWithFileName:(NSString*)aFilename
// 创建一个可变数据对象用于更新二进制数据,如字节数组
NSMutableData *pdfData = [NSMutableData data];
// 将pdf转换器指向可变数据对象和要转换的UIView
UIGraphicsBeginPDFContextToData(pdfData, aView.bounds, nil);
UIGraphicsBeginPDFPage();
CGContextRef pdfContext = UIGraphicsGetCurrentContext();
// 将矩形绘制到视图中,因此这会被 UIGraphicsBeginPDFContextToData 捕获
[aView.layer renderInContext:pdfContext];
// 移除 PDF 渲染上下文
UIGraphicsEndPDFContext();
// 从 ios 设备检索文档目录
NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
NSString* documentDirectory = [documentDirectories objectAtIndex:0];
NSString* documentDirectoryFilename = [documentDirectory stringByAppendingPathComponent:aFilename];
// 指示可变数据对象将其上下文写入磁盘上的文件
[pdfData writeToFile:documentDirectoryFilename atomically:YES];
NSLog(@"documentDirectoryFileName: %@",documentDirectoryFilename);
【问题讨论】:
也许这会有所帮助:[如何在 iOS 中将 UIView 转换为 PDF?][1] [1]:***.com/questions/5443166/… 【参考方案1】:您要在该视图中创建为 pdf 的视图,请执行以下更改
NSString *fileName=@"PdfFromView.pdf";
[self createPDFfromUIView:self.view saveToDocumentsWithFileName:fileName];
-(void)createPDFfromUIView:(UIView*)aView saveToDocumentsWithFileName:(NSString*)aFilename
// Creates a mutable data object for updating with binary data, like a byte array
NSMutableData *pdfData = [NSMutableData data];
// Points the pdf converter to the mutable data object and to the UIView to be converted
UIGraphicsBeginPDFContextToData(pdfData, aView.bounds, nil);
UIGraphicsBeginPDFPage();
CGContextRef pdfContext = UIGraphicsGetCurrentContext();
// draws rect to the view and thus this is captured by UIGraphicsBeginPDFContextToData
[aView.layer renderInContext:pdfContext];
// remove PDF rendering context
UIGraphicsEndPDFContext();
// Retrieves the document directories from the iOS device
NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
NSString* documentDirectory = [documentDirectories objectAtIndex:0];
NSString* documentDirectoryFilename = [documentDirectory stringByAppendingPathComponent:aFilename];
// instructs the mutable data object to write its context to a file on disk
[pdfData writeToFile:documentDirectoryFilename atomically:YES];
NSLog(@"documentDirectoryFileName: %@",documentDirectoryFilename);
执行此代码后,转到文档文件夹,您将找到包含与您传入的视图相同内容的 pdf 文档
块引用
[self createPDFfromUIView:self.view saveToDocumentsWithFileName:fileName]
块引用
这个方法调用。 这段代码运行良好感谢 Antonio
【讨论】:
这段代码工作正常,但我没有在 pdf 文件中看到我的数据 代码正在运行,但我的 pdf 文件中没有看到任何数据 UITextView *textView=[[UITextView alloc]initWithFrame:CGRectMake(0, 0,320,250)]; textView.text=@"这是文本视图内容"; [self.view addSubview:textView]; NSString *fileName=@"PdfFromView.pdf"; [self createPDFfromUIView:self.view saveToDocumentsWithFileName:fileName]; 现在您发现 textview 内容在 pdf 文档中【参考方案2】:取自这里:
How to Convert UIView to PDF within iOS?
(void)createPDFfromUIView:(UIView*)aView saveToDocumentsWithFileName:(NSString*)aFilename
// Creates a mutable data object for updating with binary data, like a byte array
NSMutableData *pdfData = [NSMutableData data];
// Points the pdf converter to the mutable data object and to the UIView to be converted
UIGraphicsBeginPDFContextToData(pdfData, aView.bounds, nil);
UIGraphicsBeginPDFPage();
CGContextRef pdfContext = UIGraphicsGetCurrentContext();
// draws rect to the view and thus this is captured by UIGraphicsBeginPDFContextToData
[aView.layer renderInContext:pdfContext];
// remove PDF rendering context
UIGraphicsEndPDFContext();
// Retrieves the document directories from the iOS device
NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
NSString* documentDirectory = [documentDirectories objectAtIndex:0];
NSString* documentDirectoryFilename = [documentDirectory stringByAppendingPathComponent:aFilename];
// instructs the mutable data object to write its context to a file on disk
[pdfData writeToFile:documentDirectoryFilename atomically:YES];
NSLog(@"documentDirectoryFileName: %@",documentDirectoryFilename);
【讨论】:
以及如何给这个文件一个文件名 我可以称这个方法为 [self creatPDFFromView:fileName]; 这只是一个例子,复制代码并用它做你想做的事,但是如果你想将它用作方法,是的,将它称为 ex: [self creatPDFFromView:@"myFile .pdf"]; 如果你有空,我们可以聊聊这个解决方案吗 我已经尝试过了,但是在我的实现中作为您给定的代码出现异常以上是关于如何在iphone应用程序中将饼图保存为pdf文件的主要内容,如果未能解决你的问题,请参考以下文章
如何通过应用程序将 PDF 文件保存在 iPhone 设备中
如何在 Python 中将函数输出保存为 PDF? [关闭]
如何在 ASP.net Web 应用程序中将嵌入的 PDF 保存到解决方案内的文件夹中
如何在 iphone 应用程序开发中将数据从版本保存到版本?