Home > Software design >  Xcode, adding my image view to mail composer in app
Xcode, adding my image view to mail composer in app

Time:08-09

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)

CodePudding user response:

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)
}
  • Related