我正在尝试找出在电子邮件正文中添加图片的最佳方式,而不是ios中的附件。
1)Apple提供了一项功能" addAttachment "并且文档说,要在内容中添加任何图像,我们应该使用此功能,但我尝试了该功能,并发送了一封邮件,我在我的浏览器上查看,它被收到作为附件。
2)其次,许多博客都说要使用 base64编码,但这也无法正常工作,图像会以破坏的形式发送。
所以朋友们,请帮助我找到最好的解决方案来做到这一点。
此致 兰吉特
答案 0 :(得分:60)
将电子邮件格式设置为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];
夫特
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)
}
答案 1 :(得分:10)
我最近刚接触过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)
}
返回格式化图像的功能:
func returnEmailStringBase64EncodedImage(image:UIImage) -> String {
let imgData:NSData = UIImagePNGRepresentation(image)!;
let dataString = imgData.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))
return dataString
}
答案 2 :(得分:3)
我发现(至少在我的情况下)PNG将在消息编写器中工作,但不是在用户打开/接收消息时。
作曲家Dandily showing logo PNG image!
观看者Not so much logo images over here.
(偶尔图像应该是浅蓝色轮廓。)
使用下面的HTML正文字符串和下面的转换似乎可以解决问题。
使用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"];
使用JPEG数据将图像转换为字符串
+ (NSString *)dataStringFromImage:(UIImage *)image
{
NSData *imgData = UIImageJPEGRepresentation(image, 1);
return [imgData base64EncodedStringWithOptions:kNilOptions];
}
其他信息:
感谢@Richard对这个问题的正确答案。
答案 3 :(得分:1)
很少有事情需要注意: - 使用addAttachmentData - 使用setMessageBody并设置isHTML:true
您无需在电子邮件正文中手动添加。 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)}