【发布时间】:2011-02-07 08:48:43
【问题描述】:
我有一个包含多个标签和文本视图的视图。我正在尝试为该页面/视图生成 PDF 文件并附加到电子邮件中。
首先,我尝试了这种方法,但似乎文件未创建或未附加到电子邮件中。这是我的代码:
NSMutableData *pdfData = [NSMutableData data];
UIGraphicsBeginPDFContextToData(pdfData, self.view.bounds, nil);
UIGraphicsBeginPDFPage();
[self.view drawRect:self.view.bounds];
UIGraphicsEndPDFContext();
MFMailComposeViewController *mailComposer = [[[MFMailComposeViewController alloc] init] autorelease];
mailComposer.mailComposeDelegate = self;
[pdfData writeToFile:file atomically:YES];
[mailComposer addAttachmentData:pdfData mimeType:@"application/pdf" fileName:file];
[self presentModalViewController:mailComposer animated:YES];
由于这不起作用,我尝试了一种不同的方法,首先单独创建 PDF,然后将其附加到电子邮件中。然后我看到那个目录下创建了PDF文件,但是是空的!!!
这是我修改后的代码:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *file = [documentsDirectory stringByAppendingFormat:@"/tempFile.pdf"];
UIGraphicsBeginPDFContextToFile(file, self.view.bounds, nil);
UIGraphicsBeginPDFPage();
[self.view drawRect:self.view.bounds];
UIGraphicsEndPDFContext();
MFMailComposeViewController *mailComposer = [[[MFMailComposeViewController alloc] init] autorelease];
mailComposer.mailComposeDelegate = self;
[pdfData writeToFile:file atomically:YES];
[mailComposer addAttachmentData:pdfData mimeType:@"application/pdf" fileName:file];
[self presentModalViewController:mailComposer animated:YES];
- 能否请您帮我从视图中成功创建 PDF,然后将其附加到电子邮件中?
【问题讨论】:
标签: objective-c pdf-generation