Base64 HTMLembedded式图像在邮寄时不显示

我正在embedded以HTML格式进行base64编码的图像,如下所示:

[html appendFormat:@"<html><body><p><b><img src=\"data:image/png;base64,%@\"></b></p></body><html>", base64ImageString]; 

然后我创build一个新的电子邮件如下:

 MFMailComposeViewController *mailVC = [[MFMailComposeViewController alloc] init]; mailVC.mailComposeDelegate = self; [mailVC setMessageBody:html isHTML:YES]; [self presentModalViewController:mailVC animated:YES]; 

embedded的图像在发送之前会显示在新电子邮件中,但不会显示在邮件传送到的任何电子邮件客户端中。 我认为草稿中正确显示的图像表明embedded过程是成功的,但我不明白为什么它在交付时不显示。 看在交付的邮件中的原始HTML显示:src =“cid:(null)”任何帮助,将不胜感激请!

我偶然发现了同样的问题,解决办法相当复杂。 可以在电子邮件中embedded图像。 问题是,由于一些奇怪的原因base64编码图像不能包含新的行(超奇怪!我知道)。 我猜你正在使用Matt Gallagher的NSData + Base64? 我也是! 此类别创build一个多行base64string。 该类别中的代码是

 - (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的第三个参数replace为false,您将得到一行base64string,并为我工作。 我最终创build了一个新的function(只是不打破别的!)的范畴内。

 - (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工作正常。 到目前为止我testing过的电子邮件客户端都显示embedded式图像。 希望对你有帮助!

编辑:正如在评论中指出的,这个解决scheme只是部分。 该图像将作为数据URI附加在电子邮件中。 但是,并非所有的电子邮件客户端都将显示embedded式图像