親愛的請參考下面的代碼
您可以使用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)
}
}
你需要更清楚的問題,究竟是什麼問題?你有截圖嗎?你知道如何將圖像轉換爲數據嗎?你知道如何附加文件嗎?你研究了這些主題嗎?這個問題太廣泛了。 – Wain
@slevin,請參閱並嘗試此解決方案進行屏幕保護 - http://stackoverflow.com/a/29592307/5593725和http://stackoverflow.com/a/25445629/5593725 –
嗨,謝謝..我需要截取屏幕截圖並將其附加到帶有一個按鈕的新郵件。 – slevin