从“图像视图”发送带附件的电子邮件

时间:2016-01-26 14:57:56

标签: ios swift email attachment

好的,我现在进一步了。我在应用程序中使用了电子邮件按钮,但它没有在电子邮件中添加我在UIImageview中选择的图片。有人能告诉我如何将所选图片添加到电子邮件正文中吗?

@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)
}
    // start e-mail

    @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)
    }
}

2 个答案:

答案 0 :(得分:1)

您应该查看MFMailComposeViewController的文档 您可以使用setToRecipients:addAttachmentData:mimeType:fileName:

设置接收者和附件

答案 1 :(得分:0)

尝试使用此代码填充您的电子邮件功能:

func configuredMailComposeViewController() -> MFMailComposeViewController {
    let mailComposerVC = MFMailComposeViewController()
    mailComposerVC.mailComposeDelegate = self // Extremely important to set the --mailComposeDelegate-- property, NOT the --delegate-- property

    mailComposerVC.setToRecipients(["nurdin@gmail.com"])
    mailComposerVC.setSubject("Sending you an in-app e-mail...")
    mailComposerVC.setMessageBody("Sending e-mail in-app is not so bad!", isHTML: false)

    return mailComposerVC
}

此代码位于您的操作函数或您要添加的位置...

let mailComposeViewController = configuredMailComposeViewController()
    if MFMailComposeViewController.canSendMail() {
        // On Success
    } else {
        // Error handling here
    }