如何将ImageView中的图像附加到应用程序中的电子邮件

时间:2016-01-27 11:23:05

标签: ios swift email attachment

得到以下内容。我制作了一个简单的IOS / Swift应用程序,需要将图像发送到特定的电子邮件。我已经开始工作了:

  1. 拍照
  2. 从现有照片中获取图片
  3. 图像显示在图像视图
  4. 发送按钮,引导我发送已配置的邮件:收件人,主题和邮件正文。
  5. 我需要工作的是如何在按下发送时将图像视图中的选定图像添加到电子邮件中。

    以下代码是我使用的代码:

    用于拍摄和选择图像:

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

    任何想法都会很棒!

1 个答案:

答案 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
}