如何将viewcontroller的屏幕截图附加到邮件中?我已经有了发送邮件的代码......
@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)
}
答案 0 :(得分:1)
亲爱的请参考以下代码
您可以将MFMailComposer与文件附件一起使用
使用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)
}
作为附件提交
@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)
}
}
答案 1 :(得分:1)
在UIView上创建一个可以截取屏幕截图的扩展程序:
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
}
}
您可以在任何视图上使用此功能来创建屏幕截图。然后按照提供的其他答案发送电子邮件。
答案 2 :(得分:0)
@Slevin你只需要将你拥有的图像(截图)附加到MFMailComposer对象
//imageObject is image object
var myData: NSData = UIImagePNGRepresentation(imageObject)
picker.addAttachmentData(myData, mimeType: "image/png", fileName: "image.png")
您可以使用以下代码将图像作为数据附加到MFMailComposer对象
答案 3 :(得分:0)
迅速5: 超级简单。
首先创建UIImage变量:
var image1 = UIImage?
第二个创建简单的功能来保存屏幕截图:
func saveScreenShot() {
let renderer = UIGraphicsImageRenderer(size: view.bounds.size)
let pieImage = renderer.image { ctx in
view.drawHierarchy(in: view.bounds, afterScreenUpdates: true)
}
}
现在更新您的邮件功能:
@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)
}