问题描述
我正在尝试找出在电子邮件正文中添加图片的最佳方式,而不是ios中的附件。
I am trying to find out the best way to add an image inside the body of the email and not as attachment in ios.
1)Apple提供了一个功能 addAttachment ,并且文档说,要在内容中添加任何图像,我们应该使用此功能,但我尝试了这个功能,并发了一封邮件,我在我的浏览器上查了一下,它被收到了附件。
1) Apple has provided a function "addAttachment" and the doc says, to add any image in the content, we should use this function, but I tried that function, and sent an mail, I checked on my browser, it is recieved as an attachment.
2)其次,许多博客说使用 base64编码,,但也无法正常工作,图片会以破损的形式发送。
2) Secondly, many blogs say to use base64 encoding, but that also wont work, image is sent as a broken one.
所以朋友们,请帮助我找到最好的这样做的解决方案。
So friends, please help me out to find the best available solution to do this.
问候
Ranjit
RegardsRanjit
推荐答案
将电子邮件格式设置为HTML。这个代码在我的应用程序中很好。
Set email format as HTML. This code is woking fine in my app.
MFMailComposeViewController *emailDialog = [[MFMailComposeViewController alloc] init];
NSString *htmlMsg = @"<html><body><p>This is your message</p></body></html>";
NSData *jpegData = UIImageJPEGRepresentation(emailImage, 1.0);
NSString *fileName = @"test";
fileName = [fileName stringByAppendingPathExtension:@"jpeg"];
[emailDialog addAttachmentData:jpegData mimeType:@"image/jpeg" fileName:fileName];
emailDialog setSubject:@"email subject"];
[emailDialog setMessageBody:htmlMsg isHTML:YES];
[self presentModalViewController:emailDialog animated:YES];
[emailDialog release];
Swift
import MessageUI
func composeMail() {
let mailComposeVC = MFMailComposeViewController()
mailComposeVC.addAttachmentData(UIImageJPEGRepresentation(UIImage(named: "emailImage")!, CGFloat(1.0))!, mimeType: "image/jpeg", fileName: "test.jpeg")
mailComposeVC.setSubject("Email Subject")
mailComposeVC.setMessageBody("<html><body><p>This is your message</p></body></html>", isHTML: true)
self.presentViewController(mailComposeVC, animated: true, completion: nil)
}
这篇关于如何使用MFMailComposeViewController在电子邮件正文中添加图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!