2012-04-20 53 views
0

我有iPhone应用程序创建了饼图我希望图表应该保存在iPhone中的PDF文件。如何在iPhone应用程序的PDF文件中保存饼图图

下面是饼图的代码,但我怎么能保存为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(); 

//绘制矩形的视图,因此,这是通过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); 
} 
+0

也许这可以帮助: [如何转换的UIView内的iOS PDF?] [1] [1]:HTTP://计算器。com/questions/5443166/how-to-convert-uiview-to-pdf-within-ios – CarlJ 2012-04-20 09:05:19

回答

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

可能在代码 – 2012-04-20 09:18:53

+0

下面添加下面的代码以及如何将文件名赋予此文件afilename – 2012-04-20 09:19:54

+0

aFileName = @“myName.pdf” – 2012-04-20 09:21:10

1

要创建为在浏览PDF哪种观点做了以下更改

NSString *[email protected]"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文档

[自createPDFfromUIView:self.view saveToDocumentsWithFileName:文件名]

Blockquote

此方法调用。 这个代码工作的罚款感谢安东尼奥

+0

此代码工作正常,但我没有看到在PDF文件 – 2012-04-20 11:22:05

+0

的代码工作我的数据,但我没有看到我的PDF文件 – 2012-04-20 11:35:42

+0

的UITextView *的TextView = [[UITextView的页头] initWithFrame任何数据:CGRectMake(0 ,0,320,250)]; \t textView.text = @“这是Textview内容”; \t [self.view addSubview:textView]; NSString * fileName = @“PdfFromView.pdf”; [self createPDFfromUIView:self.view saveToDocumentsWithFileName:fileName]; **现在你发现textview的内容是在pdf文档** – Yuva 2012-04-21 08:19:34

相关问题