0

我想从我正在使用以下代码的视图中创建 pdf,我从网上获得了这个,但是如何调用此方法以及在哪里提供用于创建保存在文档文件夹中的 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

这是工作代码....不要忘记添加 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 回答
0

如果您在一个类中实现了该方法,并且从同一个类中调用它。你会这样做:

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

这会将您UIView的 PDF 文件保存在您的Documents目录中,文件名为my_pdf.pdf.

希望这可以帮助。

于 2012-06-11T07:26:59.530 回答