Reputation: 11
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)
}
Upvotes: 1
Views: 1817
Reputation: 43
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)
}
Upvotes: 0
Reputation: 5275
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.
Upvotes: 1
Reputation: 501
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)
}
}
Upvotes: 1
Reputation: 1266
@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
Upvotes: 0