【问题标题】:Creating PDF from UIView in iphone app gives exception在 iphone 应用程序中从 UIView 创建 PDF 会出现异常
【发布时间】:2013-05-12 03:45:54
【问题描述】:

我正在使用以下代码从 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”

【问题讨论】:

  • 这是什么异常请分享一下。
  • @Impossible 这里是由于未捕获的异常 'NSInvalidArgumentException' 而终止应用程序的异常,原因:'-[MainViewController createPDFfromUIView:]: unrecognized selector sent to instance 0x613a950'
  • 您必须通过函数调用传递 pdf 名称,例如 [self createPDFfromUIView:self.view saveToDocumentsWithFileName:@"Test"];
  • @Impossible 再次异常终止应用程序由于未捕获的异常'NSInvalidArgumentException',原因:'-[MainViewController createPDFfromUIView::]:无法识别的选择器发送到实例 0x6156660'
  • @Impossible 感谢它的工作,但它不显示全部内容它只是显示视图的一部分

标签: iphone ios pdf uiview


【解决方案1】:

你的实现说明了一切

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

需要 2 个参数,而您只指定了 1 个,所以不是

[self createPDFfromUIView:self.view];

试试

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

【讨论】:

  • 是的,它正在工作,但是当我 NSLog 文件时它显示路径,当找到该路径时它不显示该路径
  • 它正在创建 pdf,但它不显示整个视图它只显示一半我有一个视图,其中我有滚动视图,我在滚动视图中添加内容,所以它显示所有视图跨度>
  • @AliImran:我从未尝试从UIScrollView 创建 pdf 文件。但似乎其他人也面临同样的问题。希望这可以帮助你。 stackoverflow.com/questions/15553895/…
  • 你能在我的代码中编辑我如何使用这个我不清楚你给出的链接
猜你喜欢
  • 2013-05-12
  • 1970-01-01
  • 1970-01-01
  • 2011-08-29
  • 1970-01-01
  • 2011-10-01
  • 2023-03-20
  • 1970-01-01
  • 2017-02-27
相关资源
最近更新 更多