2011-02-07 68 views
0

我對Objective C超級新手,所以請幫助我。從視圖中生成PDF以便發送電子郵件

我有幾個標籤和文本視圖的視圖。我正在嘗試爲該頁面/視圖生成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,然後將其附加到電子郵件?

我嘗試了幾種方法,但無法弄清楚我做錯了什麼。非常感謝!!!

+0

嗨!任何解決方案?謝謝! – Frade 2012-08-27 11:46:51

回答

3
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]; 

注意: 不知道繪製矩形工程views.For我它不工作! 無需將數據寫入到file.you可以直接做到這一點,而郵件是由作爲

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

試試這個.The MIME類型只需要串pdf.Also包括擴展名的文件名的文件。 PDF格式。

快樂編碼:)

相關問題