如何在iphone应用程序中以pdf文件保存饼图
我有一个iphone应用程序,它创建了一个饼图,我想这个饼图应该保存在iphone的pdf文件中 下面是PieChart的代码,但我如何将其保存在pdf中我已经读到,我们可以将文本保存在pdf中,但如何保存如何在iphone应用程序中以pdf文件保存饼图,iphone,Iphone,我有一个iphone应用程序,它创建了一个饼图,我想这个饼图应该保存在iphone的pdf文件中 下面是PieChart的代码,但我如何将其保存在pdf中我已经读到,我们可以将文本保存在pdf中,但如何保存 -(void)createGraph{ PieClass *myPieClass=[[PieClass alloc]initWithFrame:CGRectMake(400,40, 320, 230)]; myPieClass.itemArray=[[NSArray al
-(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();
//将rect绘制到视图中,因此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);
}
从这里开始:
要在该视图中创建为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.viewsaveToDocumentsWithFileName:fileName]
大宗报价
此方法调用。
多亏Antonio,这段代码工作正常。[如何在iOS内将UIView转换为PDF?][1][1]:以及如何为此文件指定文件名,我可以称此方法为[self-CreatePdfFromView:fileName];这只是一个示例,复制代码并使用它做您想做的事情,但是如果您想将其用作一种方法,可以这样调用它,例如:[self-CreatePdfFromView:@“myFile.pdf”];如果您有空,我们可以在此聊天以讨论此解决方案吗?我已经尝试过此方法,但由于您的给定代码与我的实现一起出现异常。此代码工作正常,但在pdf文件中看不到我的数据代码正常,但在我的pdf文件中没有看到任何数据UITextView*textView=[[UITextView alloc]initWithFrame:CGRectMake(0,0320250)]; text=@“这是textView内容”;[self.view addSubview:textView];NSString*fileName=@“PdfFromView.pdf”;[self-createPDFfromUIView:self.view saveToDocumentsWithFileName:fileName];现在,您发现textview内容位于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);