2013-04-30 64 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

相關問題