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,然後將其附加到電子郵件?
我嘗試了幾種方法,但無法弄清楚我做錯了什麼。非常感謝!!!
嗨!任何解決方案?謝謝! – Frade 2012-08-27 11:46:51