ios 如何使用 MFMailComposeViewController 在电子邮件正文中添加图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12210571/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add a image in email body using MFMailComposeViewController
提问by Ranjit
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.
我试图找出在电子邮件正文中添加图像而不是作为 ios 附件的最佳方法。
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.
1)Apple提供了一个函数“ addAttachment”,文档说,要在内容中添加任何图像,我们应该使用这个函数,但我尝试了该函数,并发送了一封邮件,我在浏览器上检查,收到为一个附件。
2) Secondly, many blogs say to use base64 encoding,but that also wont work, image is sent as a broken one.
2)其次,很多博客都说要使用base64编码,但这也行不通,图像作为损坏的图像发送。
So friends, please help me out to find the best available solution to do this.
所以朋友们,请帮我找到最好的解决方案来做到这一点。
Regards Ranjit
问候兰吉特
回答by msk
Set email format as HTML. This code is woking fine in my app.
将电子邮件格式设置为 HTML。这段代码在我的应用程序中运行良好。
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)
}
回答by Richard
I just went through this recently for Swift.
我最近刚刚为 Swift 经历了这个。
Function to add photo to email in Swift:
在 Swift 中将照片添加到电子邮件的功能:
func postEmail() {
var mail:MFMailComposeViewController = MFMailComposeViewController()
mail.mailComposeDelegate = self
mail.setSubject("your subject here")
var image = // your image here
var imageString = returnEmailStringBase64EncodedImage(image)
var emailBody = "<img src='data:image/png;base64,\(imageString)' width='\(image.size.width)' height='\(image.size.height)'>"
mail.setMessageBody(emailBody, isHTML:true)
self.presentViewController(mail, animated: true, completion:nil)
}
Function to return the formatted image:
返回格式化图像的函数:
func returnEmailStringBase64EncodedImage(image:UIImage) -> String {
let imgData:NSData = UIImagePNGRepresentation(image)!;
let dataString = imgData.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))
return dataString
}
回答by Andrew
I've found that (at least in my case) a PNG will work in the message composer but NOT when the message is opened / received by the user.
我发现(至少在我的情况下)PNG 将在消息编辑器中工作,但在用户打开/接收消息时无效。
Composer Dandily showing logo PNG image!
Composer Dandily 显示徽标 PNG 图像!
Viewer Not so much logo images over here.
查看器 这里没有那么多徽标图像。
(Occasionally there will be a light blue outline where the image should be.)
(有时,图像应该出现的位置会有一个浅蓝色轮廓。)
Using the HTML body string below and the conversion below that seems to do the trick.
使用下面的 HTML 正文字符串和下面的转换似乎可以解决问题。
Message Body HTML String using JPEG
使用 JPEG 的消息正文 HTML 字符串
NSString *body = [NSString stringWithFormat:
@"\
<html>\
<body>\
Check out the App!\
<br>\
Isn't this a terriffic logo?!.\
<br>\
<img src = \"data:image/jpeg;base64,%@\" width = 100 height= 100>\
<br>\
<a href = \"%@\" > CLICK ITTTTTTT! </a>\
</body>\
</html>",
imageString, @"http://www.LOLamazingappLOL.com"];
Convert Image to string with JPEG Data
使用 JPEG 数据将图像转换为字符串
+ (NSString *)dataStringFromImage:(UIImage *)image
{
NSData *imgData = UIImageJPEGRepresentation(image, 1);
return [imgData base64EncodedStringWithOptions:kNilOptions];
}
Additional Info:
附加信息:
- iOS Target = 8.0
- iOS Device = 9.1
- I am awful with HTML!
- iOS 目标 = 8.0
- iOS 设备 = 9.1
- 我对 HTML 很糟糕!
Thank you @Richard for the CORRECT answer to this question.
谢谢@Richard 对这个问题的正确回答。
回答by Ansyar Hafid
Few things to note: - Use addAttachmentData - use setMessageBody and set isHTML:true
需要注意的几点: - 使用 addAttachmentData - 使用 setMessageBody 并设置 isHTML:true
you dont have to add manually in your email body. the api will take care of that.
您不必在电子邮件正文中手动添加。api 会解决这个问题。
func postEmail() {
var mail:MFMailComposeViewController = MFMailComposeViewController()
mail.mailComposeDelegate = self
mail.setSubject("your subject here")
var image = // your image here
var imageData = UIImageJPEGRepresentation(image, 1)
mail.addAttachmentData(imageData, mimeType:"image/jpeg", fileName:"Your Filename"
var emailBody = "<html><body><p>This is your message</p></body></html>"
mail.setMessageBody(emailBody, isHTML:true)
self.presentViewController(mail, animated: true, completion:nil)}