user4865920
user4865920

Reputation:

Attach an Image to a email button

I am trying to attach an image .png to an email
Anyone can tell me what Am I doing wrong? Thanks in advance....

    var myUIImage : UIImage!
        @IBAction func shareThisApp(sender: AnyObject) {  
            let mailComposeViewController = ShareAppEmail()
            if MFMailComposeViewController.canSendMail() {

//self.presentViewController(mailComposeViewController, animated: true, completion: nil)

//The line below is showing Error: fatal error: unexpected found nil while unwrapping an Optional value
            let imageData = UIImagePNGRepresentation(myUIImage)

            mailComposeViewController.addAttachmentData(imageData!, mimeType: "image/png", fileName: "image.png")
            } else {
                self.showSendMailErrorAlert()

//self.presentViewController(mailComposeViewController, animated: true, completion: nil)
            }
        }
        func ShareAppEmail() -> MFMailComposeViewController {
            let mailComposerVC = MFMailComposeViewController()
            mailComposerVC.mailComposeDelegate = self 

            mailComposerVC.setToRecipients([""])
            mailComposerVC.setSubject("Check this great app")
            mailComposerVC.setMessageBody("Take a look at this new app", isHTML: false)

            return mailComposerVC
        }

Upvotes: 0

Views: 331

Answers (2)

iyuna
iyuna

Reputation: 1797

Your code now looks correct except couple things. First: you should leave self.presentViewController(mailComposeViewController, animated: true, completion: nil) line and put it after attaching image to mailComposeViewController - you still need to display this controller. Second: the reason you're getting error is you haven't set value to myUIImage and trying to unwrap this value implicitly using "!" sign. If property can be nil use optional type instead. I mean myUIImage property should be UIImage? instead of UIImage! and then check if image is set up should be implemented. So final version of your code should be something like this:

var myImage: UIImage?
@IBAction func shareThisApp(sender: AnyObject) {  
    let mailComposeViewController = ShareAppEmail()
    if MFMailComposeViewController.canSendMail() {
         if let myImage = myImage,
            let myImageData = UIImagePNGRepresentation(myImage) {
             mailComposeViewController.addAttachmentData(myImageData, mimeType: "image/png", fileName: "image.png")
             presentViewController(mailComposeViewController, animated: true, completion: nil)
         } else {
             // myImage is not set or have wrong format
         }
   } else {
       self.showSendMailErrorAlert()
   }
}
// dropping func ShareAppEmail() cause everything seems correct there

Upvotes: 1

sarsparillo
sarsparillo

Reputation: 53

You haven't yet set what mailComposerVC is - you're telling your code to use a line that hasn't yet been defined. Since Swift 1.2, you have to define what a constant is before you can use it. Try moving your let... statement further up in your code, or using a constant you defined earlier.

Upvotes: 1

Related Questions