在iphone应用程序中从UIView创建PDF会出现异常

时间:2013-05-17 03:58:05

标签: iphone ios pdf uiview

我正在使用以下代码从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'而终止app的原因:' - [MainViewController createPDFfromUIView:]:无法识别的选择器发送到实例0x613a950'

1 个答案:

答案 0 :(得分:2)

您的实施说明了一切

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

预计您指定的2个参数仅为1,因此不是

[self createPDFfromUIView:self.view];

尝试

[self createPDFfromUIView:self.view saveToDocumentsWithFileName:@"Name of the file"]