0

次のコードを使用しているビューからPDFを作成したいのですが、これはWebから取得しましたが、このメソッドの呼び出し方法と、ドキュメントフォルダーに保存されているPDFを作成するためのファイル名を指定する場所です。

このメソッドを呼び出すと、認識されないセレクターが送信されて例外が発生します。

  [self createPDFfromUIView:someView saveToDcumentsWithFileName:@"my_pdf.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);
 }
4

2 に答える 2

0

そのメソッドをクラスに実装していて、同じクラスから呼び出していた場合。あなたはそうするでしょう:

[self createPDFfromUIView:someView saveToDcumentsWithFileName:@"my_pdf.pdf"];

UIViewこれにより、ファイル名が。のPDFとしてDocumentsディレクトリに保存されますmy_pdf.pdf

お役に立てれば。

于 2012-06-11T07:26:59.530 に答える
0

これは機能するコードです....QuartzCore.hを追加することを忘れないでください

 #include <QuartzCore/QuartzCore.h>

 - (void)viewDidLoad
 {
   [super viewDidLoad];


     [self createPDFfromUIView:self.view saveToDocumentsWithFileName:@"abc.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-06-11T09:36:12.120 に答える