Objective c 从视图生成PDF以发送电子邮件

Objective c 从视图生成PDF以发送电子邮件,objective-c,pdf-generation,Objective C,Pdf Generation,我对目标C非常陌生,所以请帮帮我 我有一个带有多个标签和文本视图的视图。我正在尝试为该页面/视图生成PDF文件并附加到电子邮件 首先,我尝试了这种方法,似乎文件没有创建或没有附加到电子邮件。这是我的密码: NSMutableData *pdfData = [NSMutableData data]; UIGraphicsBeginPDFContextToData(pdfData, self.view.bounds, nil); UIGraphicsBeginPDFPage(); [self.vi

我对目标C非常陌生,所以请帮帮我

我有一个带有多个标签和文本视图的视图。我正在尝试为该页面/视图生成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,然后将其附加到电子邮件中吗
我试过好几种方法,但不知道我做错了什么。非常感谢

NSMutableData *pdfData = [NSMutableData data];
UIGraphicsBeginPDFContextToData(pdfData, self.view.bounds, nil);
UIGraphicsBeginPDFPage();

[self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; 

UIGraphicsEndPDFContext();

MFMailComposeViewController *mailComposer = [[[MFMailComposeViewController alloc] init] autorelease];

mailComposer.mailComposeDelegate = self;

[mailComposer addAttachmentData:pdfData mimeType:@"pdf" fileName:@"file.pdf"];

[self presentModalViewController:mailComposer animated:YES];
注: 不知道draw rect对视图有效。对我来说它不起作用! 无需将数据写入文件。您可以在邮件按如下方式编写时直接执行此操作

[mailComposer addAttachmentData:pdfData mimeType:@"pdf" fileName:@"file.pdf"];
尝试此操作。mime类型只需要字符串pdf。还包括文件名file.pdf的扩展名

快乐编码:)

注: 不知道draw rect对视图有效。对我来说它不起作用! 无需将数据写入文件。您可以在邮件按如下方式编写时直接执行此操作

[mailComposer addAttachmentData:pdfData mimeType:@"pdf" fileName:@"file.pdf"];
尝试此操作。mime类型只需要字符串pdf。还包括文件名file.pdf的扩展名


快乐编码:)

嗨!有什么解决办法吗??谢谢你好有什么解决办法吗??谢谢