如何在 App 中将 ImageView 中的图像附加到电子邮件

Posted

技术标签:

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

得到以下内容。我制作了一个简单的 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)
    

任何想法都会很棒!

【问题讨论】:

【参考方案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"

以上是关于如何在 App 中将 ImageView 中的图像附加到电子邮件的主要内容,如果未能解决你的问题,请参考以下文章

如何在linearLayout中将视图置于最前面

如何在 Kotlin 中将 View 转换为 Imageview [重复]

如何在android中将图像从一个活动发送到另一个活动?

如何在 Final Android App 中的 WebView 上添加 Imageview

无法在 Xamarin Android 中将图像从资源设置为自定义 ImageView?

如何在 Scriptable App 中将 Base64 转换为图像