从您的应用发送电子邮件,并在Swift中附加图片 [英] Sending an email from your app with an image attached in Swift
问题描述
我尝试了很多教程来在您的应用程序中发送电子邮件,但是我没有看到任何教程显示如何使用它发送图像。我正在从.WriteToFile恢复图像,该图像设置为UIImageView。我应该如何发送带有照片的电子邮件?
I've tried a lot of tutorials on sending emails in your app but none of the ones I've seen show how to send an image with it. I'm recovering an image from .WriteToFile, This image is set to a UIImageView. How should I send an email with my picture?
Niall
推荐答案
您需要在邮件中添加 attachmentData
,将图像
编码为NSData。这是一个示例,向您展示如何发送图像
的电子邮件。我想您有一个 UIViewController
,您可以在其中放置函数 sendMail
。
You need to add an attachmentData
to your mail, encoding your image
in an NSData. This is an example that show you how to send an email with
your image. I'm suppose that you have a UIViewController
where you can put the function sendMail
.
import MessageUI
class MyViewController: UIViewController, MFMailComposeViewControllerDelegate
{
// .... your stuff
func sendMail(imageView: UIImageView) {
if MFMailComposeViewController.canSendMail() {
let mail = MFMailComposeViewController()
mail.mailComposeDelegate = self;
mail.setCcRecipients(["yyyy@xxx.com"])
mail.setSubject("Your messagge")
mail.setMessageBody("Message body", isHTML: false)
let imageData: NSData = UIImagePNGRepresentation(imageView.image)!
mail.addAttachmentData(imageData, mimeType: "image/png", fileName: "imageName.png")
self.presentViewController(mail, animated: true, completion: nil)
}
}
}
为了解散VC,请包括您的 ViewController
中的以下方法:
In order to dismiss the VC, include the following method in your ViewController
:
func mailComposeController(controller: MFMailComposeViewController,
didFinishWithResult result: MFMailComposeResult, error: NSError?) {
controller.dismissViewControllerAnimated(true, completion: nil)
}
此方法的文档在 MFMailComposeViewControllerDelegate 。
这篇关于从您的应用发送电子邮件,并在Swift中附加图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!