【问题标题】:How to attach image from ImageView to E-mail within an App如何在 App 中将 ImageView 中的图像附加到电子邮件
【发布时间】:2016-05-04 07:45:01
【问题描述】:

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

  1. 拍照
  2. 从现有照片中获取图片
  3. 图像显示在图像视图中
  4. 发送按钮将我引导至已配置的邮件:收件人、主题和邮件正文。

我需要开始工作的是,当我按下发送时,如何将图像视图中选择的图像添加到电子邮件中。

以下代码是我使用的:

用于拍摄和选择图像:

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

任何想法都会很棒!

【问题讨论】:

    标签: ios swift email attachment


    【解决方案1】:
    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
    }
    

    【讨论】:

    • 最好坚持使用 .png,它们的质量更好。为此,请执行let data = UIImagePNGRepresentation(image)。其他都一样,记得设置mimeType: "image/png"
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-06
    • 2016-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多