如何使用 MFMailComposeViewController 在电子邮件正文中添加图像 [英] How to add a image in email body using MFMailComposeViewController

查看:31
本文介绍了如何使用 MFMailComposeViewController 在电子邮件正文中添加图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找出在电子邮件正文中添加图像而不是在 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.

问候兰吉特

推荐答案

设置邮件格式为 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];

斯威夫特 5

import MessageUI

    func composeMail() {

        let mailComposeVC = MFMailComposeViewController()

        mailComposeVC.addAttachmentData(UIImage(named: "emailImage")!.jpegData(compressionQuality: 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.present(mailComposeVC, animated: true, completion: nil)
    }

这篇关于如何使用 MFMailComposeViewController 在电子邮件正文中添加图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆