2016-03-01 34 views
1

我如何將視圖控制器的屏幕截圖附加到郵件中?我已經將代碼發送郵件...Xcode swift發送帶截圖的郵件?

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

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

你需要更清楚的問題,究竟是什麼問題?你有截圖嗎?你知道如何將圖像轉換爲數據嗎?你知道如何附加文件嗎?你研究了這些主題嗎?這個問題太廣泛了。 – Wain

+0

@slevin,請參閱並嘗試此解決方案進行屏幕保護 - http://stackoverflow.com/a/29592307/5593725和http://stackoverflow.com/a/25445629/5593725 –

+0

嗨,謝謝..我需要截取屏幕截圖並將其附加到帶有一個按鈕的新郵件。 – slevin

回答

0

@Slevin你只需要像(截圖),你必須附加到MFMailComposer對象

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

您可以使用下面的代碼將圖像附加爲數據的MFMailComposer對象

1

親愛的請參考下面的代碼

您可以使用MFMailComposer與文件附件

添加圖像

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

您可以使用此:使用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

創建於UIView的一個擴展,將採取截圖電子郵件正文任何視圖來創建截圖。然後按照發送電子郵件的其他答案。