-1

I am hoping someone can help me with something I've not done before. which is attach an image from a image view and put it into my email in app. i have all other fields working correctly I just cannot add my image.

thanks in advance for help

class SecondViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

@IBOutlet var imageView: UIImageView!
 

func showMailComposer() {

    guard MFMailComposeViewController.canSendMail() else {
        return
    }
    
    let composer = MFMailComposeViewController()
    composer.mailComposeDelegate = self
    composer.setToRecipients(["LJDNSGKJNDGJ"])
    composer.setSubject("JSABFKASJ")
    composer.setMessageBody("hbdfnjbqeashfbnwdlskm", isHTML: false)
    

//I would like to know how to connect the "@IBOutlet var imageView: UIImageView!" to the composer. here ? and this would then present the image in the email.

    )
    
    present(composer, animated: true)
Fishfood96
  • 13
  • 2
  • Did you try searching at all??? The very first result in my search gives the answer: https://stackoverflow.com/questions/20204495/mfmailcomposeviewcontroller-image-orientation – matt Aug 08 '22 at 12:10

1 Answers1

0
func mailButtonPress() {
   if let image = imageView!.image {
      composeMail(with:image)
   } else {
      print("image is nil")
   }
}


func composeMail(with image:UIImage) {

    let mailComposeVC = MFMailComposeViewController()

    mailComposeVC.addAttachmentData(image.jpegData(compressionQuality: CGFloat(1.0))!, mimeType: "image/jpeg", fileName:  "test.jpeg")

    mailComposeVC.setSubject("Email Subject")

    mailComposeVC.setMessageBody("<html><body><p>This is your message</p></body></html>", isHTML: true)

    self.present(mailComposeVC, animated: true, completion: nil)
}
Zeeshan Ahmad II
  • 1,047
  • 1
  • 5
  • 9