[英]Base64 HTML embedded images not showing when mailed
我正在嵌入已经在HTML中编码base64的图像,如下所示:
[html appendFormat:@"<html><body><p><b><img src=\"data:image/png;base64,%@\"></b></p></body><html>", base64ImageString];
然后我创建一个新的电子邮件如下:
MFMailComposeViewController *mailVC = [[MFMailComposeViewController alloc] init];
mailVC.mailComposeDelegate = self;
[mailVC setMessageBody:html isHTML:YES];
[self presentModalViewController:mailVC animated:YES];
嵌入的图像会在发送之前显示在新电子邮件中,但不会显示在邮件传递到的任何电子邮件客户端中。 我认为草图中正确显示图像的事实表明嵌入过程是成功的,但我不明白为什么它在交付时没有显示。 查看交付邮件中的原始HTML显示:src =“cid:(null)”任何帮助将不胜感激!
我偶然发现了同样的问题,解决方案相当复杂。 可以在电子邮件中嵌入图像。 问题是,由于一些奇怪的原因,base64编码的图像不能包含新行(超奇怪!我知道)。 我猜你正在使用Matt Gallagher的NSData + Base64? 我也是! 此类别创建多行base64字符串。 该类别中的代码是
- (NSString *)base64EncodedString
{
size_t outputLength;
char *outputBuffer =
NewBase64Encode([self bytes], [self length], true, &outputLength);
NSString *result =
[[NSString alloc]
initWithBytes:outputBuffer
length:outputLength
encoding:NSASCIIStringEncoding];
free(outputBuffer);
return result;
}
通过将NewBase64Encode的第三个参数替换为false,您将获得单行base64字符串,这对我有用。 我最终在类别中创建了一个新功能(只是为了不打破其他任何东西!)。
- (NSString *)base64EncodedStringSingleLine
{
size_t outputLength;
char *outputBuffer =
NewBase64Encode([self bytes], [self length], false, &outputLength);
NSString *result =
[[NSString alloc]
initWithBytes:outputBuffer
length:outputLength
encoding:NSASCIIStringEncoding];
free(outputBuffer);
return result;
}
使用此函数对UIImage的NSData进行编码工作正常。 我到目前为止测试的电子邮件客户端都显示了嵌入的图像。 希望这对你有用!
编辑:正如评论中所指出的,这个解决方案只是部分的。 该图像将作为数据URI附加到电子邮件中。 但是,并非所有电子邮件客户端都会显示嵌入的图像。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.