我正在制作一个能够从其中发送电子邮件的IOS应用程序。在电子邮件方法中,我使用以下内容:
let imageString = returnEmailStringBase64EncodedImage(image: ImageView.image!)
let mail = MFMailComposeViewController()
mail.mailComposeDelegate = self as MFMailComposeViewControllerDelegate
mail.setToRecipients([KBEmail.text!])
mail.setSubject("SBSS Knowledgebase Record: " + KBTitle.text!)
func returnEmailStringBase64EncodedImage(image:UIImage) -> String {
let imgData:NSData = UIImagePNGRepresentation(image)! as NSData;
let dataString = imgData.base64EncodedString(options: NSData.Base64EncodingOptions(rawValue: 0))
return dataString
}
并在我添加的HTML字符串中:
<img src='data:image/png;base64,\(imageString)'>
当我向自己发送电子邮件时,我会在MacBook Pro,iPad和iPhone 6上收到相同的电子邮件。在iPad和iPhone上,电子邮件会按照我想要的方式显示。在Mac和Android设备上,它显示HTML和编码字符串,如下所示:
--Apple-Mail-4012341F-CA6F-4B5E-A556-2AF6b4AFCC0
Content-Type: text/html;
charset=utf-8
Content-Transfer-Encoding: quoted-printable
<html><head><meta http-equiv=3D"content-type" content=3D"text/html; charset=3D=
utf-8"></head><body dir=3D"auto"><div><table><tbody><tr><td style=3D"font-fa=
mily: Arial, Helvetica, sans-serif; font-weight: bold; color: #000080; text-=
align: center;">Puffer with the fluid Giz gave me</td></tr><tr><td><hr style=
=3D"width:100%"></td></tr><tr><td style=3D"font-family: Arial, Helvetica, sa=
ns-serif; font-weight: normal; font-style: italic; text-align: center; color=
: #000080;">I think =F0=9F=A4=94 it=E2=80=99s about 6% nicotine </td></tr><t=
r><td style=3D"font-family: Arial, Helvetica, sans-serif; font-weight: bold;=
color: #000080; text-align: center;"><hr style=3D"width:100%"><img src=3D"d=
ata:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAARcAAADRCAYAAADmFfbJAAAAAXNSR0I=
Ars4c6QAAABxpRE9UAAAAAgAAAAAAAABpAAAAKAAAAGkAAABoAAEma2igPnYAAEAASURBVHgBnL0=
Hd1VXlrbbf+Le7+uu7rJdDkQlkIRIIgiJnEFESSjnnCWEAgJElEBC5JxMzsk2yQabnJ1zrirbZVd=
wBZft9z5z7XMkcNft8d3rMabX3vtkcfaz3/nOudb5t5//8FD++OmbB7L48ev7vrirN+9e06rWTaq=
av15VjRtVvWgrsV3Vi3eqdsU+zW89oobWo2pcc0qLN76kJZvOaenmC1q86bya2J+/9rRqVx1X5bK=
DKl64R/kNO5RVs1mZNZtUvGCnkktWKWZSlvoOm6k+Q6ao94CxCuwzQl2DB6tLYKSeCx6ibsFD1TN
有没有人有类似的东西,或者可以告诉我为什么会这样?
我没有此网站的权限上传图片,抱歉,所以我无法显示任何屏幕转储。
更新: 将returnEmailStringBase64EncodedImage函数替换为:
func encodeImageToBase64(image : UIImage) -> String{
let imageData : Data = UIImagePNGRepresentation(image)! as Data
let strBase64 = imageData.base64EncodedString(options: Data.Base64EncodingOptions.init(rawValue: 0))
return strBase64
}
电子邮件在MacBook,iPhone和iPad上显示OK。使用Safari和Gmail时,电子邮件看起来不错,但图片未显示。慢慢来到那里:)