在iphone应用程序中调用CreatePDF方法无效

在iphone应用程序中调用CreatePDF方法无效,iphone,xcode,uiview,Iphone,Xcode,Uiview,我想从视图中创建pdf,我正在使用以下代码我从web上获得了此代码,但如何调用此方法以及在何处提供用于创建保存在documents文件夹中的pdf的文件名 当我调用此方法时,它会发出无法识别的异常选择器 [self createPDFfromUIView:someView saveToDcumentsWithFileName:@"my_pdf.pdf"]; -(void)createPDFfromUIView:(UIView*)aView saveToDocumentsWithFil

我想从视图中创建pdf,我正在使用以下代码我从web上获得了此代码,但如何调用此方法以及在何处提供用于创建保存在documents文件夹中的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);
 }

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

[self createPDFfromUIView:someView saveToDcumentsWithFileName:@"my_pdf.pdf"];
这将把您的
ui视图
保存为您的
文档
目录中的PDF格式,文件名为,
my_PDF.PDF


希望这有帮助。

这是工作代码……别忘了添加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);
#包括
-(无效)viewDidLoad
{
[超级视图下载];
[self-createPDFfromUIView:self.view SaveToDocuments,文件名:@“abc.pdf”];
}
-(void)createPDFfromUIView:(UIView*)查看保存到文档的文件名:(NSString*)文件名
{
//创建一个可变数据对象,以便使用二进制数据(如字节数组)进行更新
NSMutableData*pdfData=[NSMutableData];
//将pdf转换器指向可变数据对象和要转换的UIView
UIGraphicsBeginPDFContextToData(pdfData,aView.bounds,nil);
UIGraphicsBeginPDFPage();
CGContextRef pdfContext=UIGraphicsGetCurrentContext();
//将rect绘制到视图中,因此UIGraphicsBeginPDFContextToData将捕获该数据
[aView.layer renderInContext:pdfContext];
//删除PDF渲染上下文
UIGraphicsSendPdfContext();
//从iOS设备检索文档目录
NSArray*documentDirectories=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,是);
NSString*documentDirectory=[documentDirectories objectAtIndex:0];
NSString*documentDirectoryFilename=[documentDirectory stringByAppendingPathComponent:aFilename];
//指示可变数据对象将其上下文写入磁盘上的文件
[pdfData WriteFile:documentDirectoryFilename原子性:是];
NSLog(@“documentDirectoryFileName:%@”,documentDirectoryFileName);

}

以这种方式调用会导致异常终止应用程序,因为无法识别的选择器发送到您编写的行。我想我们需要先创建文件。您必须将该方法放入类
@实现中。无法识别的选择器表示您没有从正确的位置调用它。我正在将其放入实现中,并在view didload方法“NSInvalidArgumentException”中调用此选择器,原因:“-[GraphViewController createPDFfromUIView:SaveToDocumentswithFileName:]:发送到实例0x6edd1d0'***的无法识别的选择器第一次抛出调用堆栈:如果仔细阅读,方法名称将为您提供所有信息。e、 g createPDFfromUIView,它将获取要从中创建PDF的UIview对象,并保存到文档SwithFileName,它告诉您,传递所需PDF文件的名称。此文件将在文档目录中创建。我这样做时,调用此方法时会出现异常,称为unrecognized selectorsent@EXC_BAD_ACCESS请问我是如何调用方法的,它是否正确