0

我正在使用以下代码从 iphone 应用程序创建 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:self.view];

我认为问题在于调用方法时的参数传递。谢谢

这是由于未捕获的异常“NSInvalidArgumentException”而导致的异常终止应用程序,原因:“-[MainViewController createPDFfromUIView:]: unrecognized selector sent to instance 0x613a950”

4

1 回答 1

2

你的实现说明了一切

-(void)createPDFfromUIView:(UIView*)aView saveToDocumentsWithFileName:(NSString*)aFilename

预计 2 个参数广告您只指定了 1 个,所以不是

[self createPDFfromUIView:self.view];

尝试

[self createPDFfromUIView:self.view saveToDocumentsWithFileName:@"Name of the file"]
于 2013-05-17T04:08:18.707 回答