2013-05-22 52 views
0

我正在使用MFMailComposerViewController從iOS應用發送電子郵件。郵件工作除了試圖添加圖像時。與我的形象的問題是讓它使用如何使用MFMailComposerViewController將UIImage添加到電子郵件?

我見過其他例子,使用類似的東西:UIImage *emailImage = [UIImage imageNamed:@"myImageName.png"]; 添加圖像。我的形象是從使用[self.photo objectForKey:kPhotoPictureKey];

 // mail 
      // Email Subject 
      NSString *emailTitle = @"Join. Download the iPhone app"; 
      // Email Content 
      NSString *messageBody = @"http://www..com/"; 
      // To address 
      NSArray *toRecipents = [NSArray arrayWithObject:@""]; 


      UIImageView *mailImage = [[UIImageView alloc] init]; 
      mailImage.image = [UIImage imageNamed:@"1.png"]; // placeholder image 
      mailImage.file = [self.photo objectForKey:kPhotoPictureKey]; 
      [mailImage loadInBackground]; 

NSString *messageBody = [NSString stringWithFormat:@"http://www.example.com/<p><b><img src='data:image/png;base64,%@'></b></p>",mailImage.image]; 


      MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init]; 
      mc.mailComposeDelegate = self; 
      [mc setSubject:emailTitle]; 
      [mc setMessageBody:messageBody isHTML:NO]; 
      [mc setToRecipients:toRecipents]; 


      // Present mail view controller on screen 
      [self presentViewController:mc animated:YES completion:NULL]; 
+0

http://stackoverflow.com/questions/1527351/how-to-add-an-uiimage-in-mailcomposer-sheet-of-mfmailcomposeviewcontroller-in-ip –

+0

**做適當的研究,你問前一個問題。** –

回答

3

使用此伴侶。它工作正常!

if ([MFMailComposeViewController canSendMail]) { 
      MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init]; 
      picker.mailComposeDelegate = self; 
      [picker setSubject:@"SUBJECT OF THE MAIL!"]; 
      NSData *myData = UIImageJPEGRepresentation(IMAGE_TO_SEND, 0.9); 
      [picker addAttachmentData:myData mimeType:@"image/jpg" fileName:@"IMAGE_NAME.jpg"]; 

      // Fill out the email body text 
      NSString *emailBody = @"BODY OF THE MAIL"; 
      [picker setMessageBody:emailBody isHTML:NO]; 
      [self presentViewController:picker animated:YES completion:nil]; 

} 
+0

工作得很好!謝謝 – hanumanDev

+0

不客氣!你能接受答案,如果它的正確答案:) – Ushan87

0

您有一個數據庫表所使用

- (void)addAttachmentData:(NSData*)attachment mimeType:(NSString*)mimeType fileName:(NSString*)filename 
Parameters 
attachment 
The data to attach. Typically, this is the contents of a file that you want to include. This parameter must not be nil. 
mimeType 
The MIME type of the specified data. (For example, the MIME type for a JPEG image is image/jpeg.) For a list of valid MIME types, see http://www.iana.org/assignments/media-types/. This parameter must not be nil. 
filename 
The preferred filename to associate with the data. This is the default name applied to the file when it is transferred to its destination. Any path separator (/) characters in the filename are converted to underscore (_) characters prior to transmission. This parameter must not be nil. 
Discussion 
This method attaches the specified data after the message body but before the user’s signature. You may attach multiple files (using different file names) but must do so prior to displaying the mail composition interface. Do not call this method after presenting the interface to the user. 

Availability 
Available in iOS 3.0 and later. 

形式MFMailComposeViewController類參考

+0

這個答案有什麼問題? +1進行補償。 –

+1

我沒有複製和粘貼代碼,讀者必須用他的大腦理解它..所以這就是爲什麼我會得到一個投票。 –

+0

當然。無需勺子餵養,至少對於這種簡單的問題,如 –

0

你可以簡單地使用這條線來附加你的圖像。

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

[mailComposer addAttachmentData:data mimeType:@"image/png" fileName:@"image.png"]; 
相關問題