1

How can i attach a screenshot from the viewcontroller to a mail? I have already the code to send the mail...

@IBAction func SendMail(sender: AnyObject) {
    let picker = MFMailComposeViewController()
    picker.mailComposeDelegate = self
    picker.setCcRecipients(["xx@xx"])
    picker.setSubject("xxx" + " " + itemName.text! + "-" + itemEtage.text! + "-" + itemRaum.text!)
    picker.setMessageBody("xx" + " " + itemNow.text! + " " + "xxx", isHTML: true)

    presentViewController(picker, animated: true, completion: nil)
}
Shantanu Gupta
  • 20,688
  • 54
  • 182
  • 286
slevin
  • 11
  • 5
  • 1
    you need to be more clear with the question, what exactly is the problem? do you have the screenshot? do you know how to convert the image to data? do you know how to attach files? did you research either of these topics? this question is too broad. – Wain Mar 01 '16 at 09:43
  • @slevin, See and Try this Solution for take a Screenshoot - http://stackoverflow.com/a/29592307/5593725 and http://stackoverflow.com/a/25445629/5593725 – PT Vyas Mar 01 '16 at 09:46
  • Hi and thanks.. i need to take a screenshot and attach this to a new mail with one button.. – slevin Mar 01 '16 at 11:16
  • Thx to all of you... now i can take a screenshot an save this to the device. Now i have to try the code below for attach – slevin Mar 01 '16 at 17:33

4 Answers4

1

Dear please refer following code

You can use MFMailComposer with file attachment

Add a image in email body using MFMailComposeViewController

 import MessageUI

func composeMail() {

    let mailComposeVC = MFMailComposeViewController()

    mailComposeVC.addAttachmentData(UIImageJPEGRepresentation(UIImage(named: "emailImage")!, 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.presentViewController(mailComposeVC, animated: true, completion: nil)

}


File as an attachment

@IBAction func sendEmail(sender: UIButton) {
    //Check to see the device can send email.
    if( MFMailComposeViewController.canSendMail() ) {
        println("Can send email.")

        let mailComposer = MFMailComposeViewController()
        mailComposer.mailComposeDelegate = self

        //Set the subject and message of the email
        mailComposer.setSubject("Have you heard a swift?")
        mailComposer.setMessageBody("This is what they sound like.", isHTML: false)

        if let filePath = NSBundle.mainBundle().pathForResource("swifts", ofType: "wav") {
            println("File path loaded.")

            if let fileData = NSData(contentsOfFile: filePath) {
                println("File data loaded.")
                mailComposer.addAttachmentData(fileData, mimeType: "audio/wav", fileName: "swifts")
            }
        }
        self.presentViewController(mailComposer, animated: true, completion: nil)
    }
}
Avinash Jadhav
  • 491
  • 4
  • 17
1

Create an extension on UIView that will take a screenshot:

extension UIView {
    func screenShot() -> UIImage {
        UIGraphicsBeginImageContextWithOptions(bounds.size, opaque, UIScreen.mainScreen().scale)
        let contextRef = UIGraphicsGetCurrentContext()
        CGContextTranslateCTM(contextRef, 0, 0)
        layer.renderInContext(contextRef!)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image
    }
}

You can use this on any view to create a screenshot. Then follow the other answers provided for sending the email.

totiDev
  • 5,189
  • 3
  • 30
  • 30
0

@Slevin you just need to attach the image(screenshot) you have to the MFMailComposer object

//imageObject is image object
var myData: NSData = UIImagePNGRepresentation(imageObject)
picker.addAttachmentData(myData, mimeType: "image/png", fileName: "image.png")

you can use the following code to attach the image as a data to the MFMailComposer object

Harshavardhan
  • 1,266
  • 2
  • 14
  • 25
0

Swift 5: It is super simply.

First create UIImage variable:

var image1 = UIImage?

Second create simple func to save screenshot:

func saveScreenShot() {
  let renderer = UIGraphicsImageRenderer(size: view.bounds.size)
  let pieImage = renderer.image { ctx in
     view.drawHierarchy(in: view.bounds, afterScreenUpdates: true)
    }
 }

Now update your mail function:

@IBAction func SendMail(sender: AnyObject) {
    saveScreenShot()
    let picker = MFMailComposeViewController()
    picker.mailComposeDelegate = self
    picker.setCcRecipients(["xx@xx"])
    picker.setSubject("xxx" + " " + itemName.text! + "-" + itemEtage.text! + "-" + itemRaum.text!)
    picker.setMessageBody("xx" + " " + itemNow.text! + " " + "xxx", isHTML: true)
    mailComposer.addAttachmentData(image1!.jpegData(compressionQuality: CGFloat(0.7))!, mimeType: "image/jpeg", fileName:  "test.jpeg")

    presentViewController(picker, animated: true, completion: nil)
}