得到以下内容。我制作了一个简单的IOS / Swift应用程序,需要将图像发送到特定的电子邮件。我已经开始工作了:
我需要工作的是如何在按下发送时将图像视图中的选定图像添加到电子邮件中。
以下代码是我使用的代码:
用于拍摄和选择图像:
@IBOutlet var imageView: UIImageView!
@IBOutlet weak var PicLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func FotoKnop(sender: AnyObject) {
}
@IBAction func chooseImageFromPhotoLibrary() {
let picker = UIImagePickerController()
picker.delegate = self
picker.sourceType = .PhotoLibrary
presentViewController(picker, animated: true, completion: nil)
}
@IBAction func chooseFromCamera() {
let picker = UIImagePickerController()
picker.delegate = self
picker.sourceType = .Camera
presentViewController(picker, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
imageView.image = image
self.dismissViewControllerAnimated(true, completion: nil)
}
用于电子邮件
@IBAction func sendEmailButtonTapped(sender: AnyObject) {
let mailComposeViewController = configuredMailComposeViewController()
if MFMailComposeViewController.canSendMail() {
self.presentViewController(mailComposeViewController, animated: true, completion: nil)
} else {
}
}
func configuredMailComposeViewController() -> MFMailComposeViewController {
let mailComposerVC = MFMailComposeViewController()
mailComposerVC.mailComposeDelegate = self // Extremely important to set the --mailComposeDelegate-- property, NOT the --delegate-- property
mailComposerVC.setToRecipients(["jvanhattem@it-serve.nl"])
mailComposerVC.setSubject("Mail vanuit PicMail")
mailComposerVC.setMessageBody("Onderstaand de doorgestuurde informatie", isHTML:
false)
return mailComposerVC
}
func showSendMailErrorAlert() {
let sendMailErrorAlert = UIAlertView(title: "Could Not Send Email", message: "Your device could not send e-mail. Please check e-mail configuration and try again.", delegate: self, cancelButtonTitle: "OK")
sendMailErrorAlert.show()
}
// MARK: MFMailComposeViewControllerDelegate Method
func mailComposeController(controller: MFMailComposeViewController!, didFinishWithResult result: MFMailComposeResult, error: NSError!) {
controller.dismissViewControllerAnimated(true, completion: nil)
}
}
任何想法都会很棒!
答案 0 :(得分:2)
func configuredMailComposeViewController() -> MFMailComposeViewController {
let mailComposerVC = MFMailComposeViewController()
mailComposerVC.mailComposeDelegate = self // Extremely important to set the --mailComposeDelegate-- property, NOT the --delegate-- property
mailComposerVC.setToRecipients(["jvanhattem@it-serve.nl"])
mailComposerVC.setSubject("Mail vanuit PicMail")
mailComposerVC.setMessageBody("Onderstaand de doorgestuurde informatie", isHTML:
false)
//Add Image as Attachment
if let image = imageView.image {
let data = UIImageJPEGRepresentation(image, 1.0)
mailComposerVC.addAttachmentData(data!, mimeType: "image/jpg", fileName: "image")
}
return mailComposerVC
}