0

私は円グラフを作成した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);
 }
4

2 に答える 2

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 のおかげで、このコードは正常に動作します

于 2012-04-20T11:08:06.163 に答える
0

ここから取った:

iOS内でUIViewをPDFに変換する方法は?

(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);
}
于 2012-04-20T09:06:58.533 に答える