2013-04-30 67 views
3

我有两个UIView需要转换为PDF并作为附件发送。我的代码与第一个UIView重叠。 我想将此作为附件发送至电子邮件。如何从两个页面创建两个UIView的pdf

谢谢。

NSMutableData *pdfData=[NSMutableData data]; 


UIGraphicsBeginPDFContextToData(pdfData,aView.bounds, nil); 
CGContextRef pdfContext=UIGraphicsGetCurrentContext(); 

UIGraphicsBeginPDFPage(); 
[aView.layer renderInContext:pdfContext]; 
UIGraphicsEndPDFContext(); 


UIGraphicsBeginPDFContextToData(pdfData, bView.bounds, nil); 

CGContextRef pdfContext2=UIGraphicsGetCurrentContext(); 
UIGraphicsBeginPDFPage(); 

[bView.layer renderInContext:pdfContext2]; 
    UIGraphicsEndPDFContext(); 



MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init]; 
mailer.mailComposeDelegate = self; 
NSString *subject=[NSString stringWithFormat:@"Delivery Report-%@",[globUserID uppercaseString]]; 
[mailer setSubject:subject]; 
[mailer addAttachmentData:pdfData mimeType:@"pdf" fileName:@"DeliveryReport.pdf"]; 
[self presentModalViewController:mailer animated:YES]; 

回答

12

不要创建第二个上下文。

NSMutableData *pdfData=[NSMutableData data]; 
CGRect bounds = CGRectMake(0, 0, 612, 792); // letter sized paper, adjust as needed 

UIGraphicsBeginPDFContextToData(pdfData, bounds, nil); 
CGContextRef pdfContext = UIGraphicsGetCurrentContext(); 

UIGraphicsBeginPDFPage(); 
[aView.layer renderInContext:pdfContext]; 

UIGraphicsBeginPDFPage(); 
[bView.layer renderInContext:pdfContext]; 

UIGraphicsEndPDFContext(); 
+0

非常感谢你的作品,任何提示,如果我想做第一页potrait第二页景观? – baste 2013-04-30 15:42:07

+0

使用UIGraphicsBeginPDFPageWithInfo函数以不同尺寸开始新页面。 – rmaddy 2013-04-30 15:43:46

+0

是否这样? 'NSMutableData * pdfData = [NSMutableData data]; CGRect bounds1 = aView.bounds; CGRect bounds2 = bView.bounds; UIGraphicsBeginPDFContextToData(pdfData,bounds1,nil); CGContextRef pdfContext = UIGraphicsGetCurrentContext(); UIGraphicsBeginPDFPage(); [aView.layer renderInContext:pdfContext]; UIGraphicsBeginPDFContextToData(pdfData,bounds2,nil); UIGraphicsBeginPDFPage(); [bView.layer renderInContext:pdfContext]; UIGraphicsEndPDFContext();' – baste 2013-04-30 15:52:25

相关问题