如何使用MFMailComposeViewController在电子邮件正文中添加图像
我试图找出最好的方式来添加一个图像内的电子邮件的正文,而不是作为附件在iOS中。
1)苹果提供了一个function“ addAttachment ”和文档说,添加任何形象的内容,我们应该使用这个function,但我试过这个function,并发送邮件,我检查了我的浏览器,它被接收为一个附件。
2)其次,许多博客都说要使用base64编码,但这也不行,图像被发送为一个破碎的。
所以朋友们,请帮我find最好的解决scheme来做到这一点。
关心Ranjit
将电子邮件格式设置为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) }
我刚刚经历了这个Swift。
在Swift中添加照片到电子邮件的function:
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 }
我发现(至less在我的情况下)PNG将在消息编辑器中工作,但不是在用户打开/接收消息时。
composer phpDandily显示徽标PNG图像!
浏览器没有那么多的标志图像在这里。
(偶尔会有一个浅蓝色的轮廓,图像应该是)。
使用下面的HTML正文string和下面的转换似乎有伎俩。
消息正文使用JPEG的HTMLstring
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数据的string
+ (NSString *)dataStringFromImage:(UIImage *)image { NSData *imgData = UIImageJPEGRepresentation(image, 1); return [imgData base64EncodedStringWithOptions:kNilOptions]; }
附加信息:
- iOS目标= 8.0
- iOS设备= 9.1
- 我用HTML很糟糕!
谢谢@理查德对这个问题的正确答案。
几件事要注意: – 使用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)}