如何在iPhone应用程序的pdf文件中保存饼图

时间:2012-04-20 09:02:01

标签: iphone

我有创建饼图的iphone应用程序我希望该图表应保存在iphone的pdf文件中。

下面是PieChart的代码,但是如何将其保存为pdf我已经读过我们可以用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();

//将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);
 }

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

从这里开始:

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);
}