Reputation: 11
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")
}
}
Upvotes: 0
Views: 133