2017-06-14 16 views
2

我在我的應用程序中努力工作,但我遇到了問題。 需要在Swift的HTML代碼中插入圖像發送電子郵件。如何在HTML Body Email Swift 3中插入圖片?

這是我的代碼,但當然不起作用!

var h = "<html><body leftmargin=15% topmargin=2% marginwidth=7% marginheight=7%>" 
h = h + "<img width=100px src=logoICR.png>" 
h = h + "</body></html>" 
+0

你見過這個https://開頭計算器。 COM /問題/ 12210571 /如何到添加-A-圖像輸入電子郵件-體使用,mfmailcomposeviewcontrolle r –

+0

我不認爲你不能用HTML標籤發送圖像。你只能作爲附件發送 –

+0

Thak你。我在電子郵件前景html中做過,但我知道圖像源,在Swift Xcode中,我知道哪裏是si? (Src = ??????) – Gilko

回答

3

請嘗試以下代碼電子郵件HTML正文裏面插入圖片。


代碼 雨燕4.0 & 雨燕3.1
import MessageUI 

class ViewController: UIViewController, MFMailComposeViewControllerDelegate{ 


    override func viewDidLoad() { 
     super.viewDidLoad() 
    } 

    //MARK: - Send Email 
    @IBAction func actionSendEmail(_ sender: Any) { 

     if !MFMailComposeViewController.canSendMail() { 
      print("Mail services are not available") 
      return 
     } 
     let image = UIImage(named:"testImage") // Your Image 
     let imageData = UIImagePNGRepresentation(image!) ?? nil 
     let base64String = imageData?.base64EncodedString() ?? "" // Your String Image 
     let strHtml = "<html><body><p>Header: Hello Test Email</p><p><b><img src='data:image/png;base64,\(String(describing: base64String))'></b></p></body></html>" 
     let composeVC = MFMailComposeViewController() 
     composeVC.mailComposeDelegate = self 
     // Configure the fields of the interface. 
     composeVC.setToRecipients(["[email protected]"]) 
     composeVC.setSubject("Hello!") 
     composeVC.setMessageBody(strHtml, isHTML: true) 

     // Present the view controller modally. 
     self.present(composeVC, animated: true, completion: nil) 
    } 

    //MARK:- MFMailCompose Delegate Methods 
    func mailComposeController(_ controller: MFMailComposeViewController, 
           didFinishWith result: MFMailComposeResult, error: Error?) { 
     // Dismiss the mail compose view controller. 
     controller.dismiss(animated: true, completion: nil) 
    } 

} 
+0

謝謝,但是這段代碼對我不起作用。我得到了我只需要的代碼ti從我的圖像galery(xcasset文件夾)找到los da圖像的方式 – Gilko

0

工作提供圖像的完整路徑:像

<img width=100px src=http://example.com/images/logoICR.png> 
+0

我不知道爲什麼,但不起作用 – Gilko