简体   繁体   English

Xcode swift 发送带有屏幕截图的邮件?

[英]Xcode swift send mail with screenshot?

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)
}

Dear please refer following code亲请参考以下代码

You can use MFMailComposer with file attachment您可以将 MFMailComposer 与文件附件一起使用

Add a image in email body using MFMailComposeViewController使用 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)
    }
}

Create an extension on UIView that will take a screenshot:在 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
    }
}

You can use this on any view to create a screenshot.您可以在任何视图上使用它来创建屏幕截图。 Then follow the other answers provided for sending the email.然后按照为发送电子邮件提供的其他答案进行操作。

@Slevin you just need to attach the image(screenshot) you have to the MFMailComposer object @Slevin 您只需要将您拥有的图像(屏幕截图)附加到 MFMailComposer 对象

//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您可以使用以下代码将图像作为数据附加到 MFMailComposer 对象

Swift 5: It is super simply. Swift 5:超级简单。

First create UIImage variable:首先创建 UIImage 变量:

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)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Xcode7:如何快速发送崩溃报告 - Xcode7: How to mail crash report in swift 从图库照片导入屏幕截图并在Xcode 6.3 Swift中设置为背景 - Import screenshot from gallery photos and set as background in Xcode 6.3 Swift 目标C-Swift桥接问题Xcode 6.3-提供的屏幕截图- - Objective C - Swift Bridging issue Xcode 6.3 -Screenshot provided- 拍摄屏幕截图,然后将其保存到相机胶卷中。 Swift 4.2,Xcode 10 - Take a screenshot and then save it to the camera roll. Swift 4.2, Xcode 10 尝试制作一个屏幕快照按钮,单击该按钮会将屏幕快照发送到我的相机胶卷。 Swift 3,Xcode 8,IOS - Trying to make a screenshot button that sends the screenshot to my camera roll, when the button is clicked. Swift 3, Xcode 8, IOS 如何在Swift中使用黑猩猩发送即时电子邮件? - How to send instant email with mail chimp in Swift? 从CollectionViewCell.swift发送邮件 - Send mail from CollectionViewCell.swift Swift如何在AppDelegate提取中发送电子邮件? - Swift how to send eMail mail in AppDelegate fetch? 我不知道如何在Swift中将屏幕截图发送到Instagram应用程序 - I don't know how to send screenshot to Instagram application in Swift pdf屏幕截图附件邮件 - pdf screenshot attachment mail
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM