0

I created an app that, with a button allows you to choose between taking a photo with the camera or picking it from the library (UIImagePickerController). When you choose the photo, then it is assigned to an ImageView. With another button I want the user to be able to compose a mail using MailComposer that is already pre-compiled (recipient, subject and message body). I also want to attach the image assigned to the web view, and I tried with this code

mailComposerVC.addAttachmentData(UIImageJPEGRepresentation(UIImage(named: "emailImage")!, CGFloat(1.0))!, mimeType: "image/jpeg", fileName:  "image")

But it doesn't work, with this error "Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value"

Thank you!

  • You are getting this because one of the explicit unwraps you are doing is actually nil. Try using guard or if let to handle nil values. It's a bad practice to use force unwrap! – Todor Brachkov Mar 06 '18 at 10:18
  • I tried with an if statement that checks if imageview is nil. The composition seems to be working, but the photo is not attached. – Tommaso Pirola Mar 06 '18 at 10:50

0 Answers0