使用MFMailComposeViewController附加图像从PhotoGallery到电子邮件

发布于 2025-02-06 07:54:15 字数 1595 浏览 1 评论 0原文

我希望用户从照片库中选择其所需的图像,然后将这些图像包含在他们将要发送的电子邮件中。

我已经创建了一个按钮,该按钮使用户可以打开图库,然后在选择图像后将其显示在UIImageView中。但是,我认为我撰写的代码是不正确的。

UIimageViews名称是ImageView

按钮以打开画廊

@IBAction func openPhotoLibraryButton(sender: AnyObject) {
    if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
        let imagePicker = UIImagePickerController()
        imagePicker.delegate = self
        imagePicker.sourceType = .photoLibrary;
        imagePicker.allowsEditing = true
        self.present(imagePicker, animated: true, completion: nil)
    }
}

电子邮件代码

@IBAction func sendEmail(_ sender: Any) {
    if MFMailComposeViewController.canSendMail() {
        let mail = MFMailComposeViewController()
        let emailFullName: String = fullName.text!
        let emailCell: String = cellNumber.text!
        let image1: UIImage = imageView.image!
        mail.mailComposeDelegate = self
        //Reciepient
        mail.setToRecipients(["[email protected]"])
        // Email body
        mail.setMessageBody("FullName: \(emailFullName) \n Cellphone No: \(emailCell)"   , isHTML: false)
        // Subject
        mail.setSubject("Application for RTO: ")
        
        // Email attachment
        mail.addAttachmentData(Data, mimeType: "image/jpg", fileName: "\(image1)")
        self.present(mail, animated: true, completion: nil)
        }
     else {
        print("Email cannot be sent")
        
    }
}

I would like for user to choose their desired image from photo gallery and then that image is included in the email that they're are about to send.

I've already created a button that enables user to open gallery and then after choosing image it is displayed in UIImageView. However, my code that I have written in addAttachment I think is incorrect.

The UIImageViews name is imageView

button to open gallery

@IBAction func openPhotoLibraryButton(sender: AnyObject) {
    if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
        let imagePicker = UIImagePickerController()
        imagePicker.delegate = self
        imagePicker.sourceType = .photoLibrary;
        imagePicker.allowsEditing = true
        self.present(imagePicker, animated: true, completion: nil)
    }
}

Email code

@IBAction func sendEmail(_ sender: Any) {
    if MFMailComposeViewController.canSendMail() {
        let mail = MFMailComposeViewController()
        let emailFullName: String = fullName.text!
        let emailCell: String = cellNumber.text!
        let image1: UIImage = imageView.image!
        mail.mailComposeDelegate = self
        //Reciepient
        mail.setToRecipients(["[email protected]"])
        // Email body
        mail.setMessageBody("FullName: \(emailFullName) \n Cellphone No: \(emailCell)"   , isHTML: false)
        // Subject
        mail.setSubject("Application for RTO: ")
        
        // Email attachment
        mail.addAttachmentData(Data, mimeType: "image/jpg", fileName: "\(image1)")
        self.present(mail, animated: true, completion: nil)
        }
     else {
        print("Email cannot be sent")
        
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文