我有这个代码,它应该将HTML文件附加到电子邮件中,并且还在电子邮件正文中显示HTML文件的内容.HTML文件中引用了一个图像(reading.png),每隔一行显示一次.但是,它不会显示在电子邮件正文中.我需要做些什么才能让它显示出来?
if ([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
mailer.mailComposeDelegate = self; // set delegate to notify us what's happen'in
[mailer setSubject:[NSString stringWithFormat: @"Site Readings for: %@", gSiteID.globalSiteID]];
// add the image for the HTML
UIImage *toolImage = [UIImage imageNamed:@"reading.png"];
NSData *imageData = UIImagePNGRepresentation(toolImage);
[mailer addAttachmentData:imageData mimeType:@"image/png" fileName:@"reading.png"];
// attach file
NSString *file = [[NSBundle mainBundle] pathForResource:@"Surveyor" ofType:@"txt"];
NSData *surveyorTxt= [NSData dataWithContentsOfFile: file];
[mailer addAttachmentData: surveyorTxt mimeType:@"text/html" fileName: @"Surveyor.txt"];
// display the file in the body of the email
NSString *reportBody;
reportBody = [[NSString alloc] initWithData:databuffer encoding:NSASCIIStringEncoding];
[mailer setMessageBody: reportBody isHTML:true]; // indicate the body is html
[self presentModalViewController: mailer animated:TRUE];
Run Code Online (Sandbox Code Playgroud)
这是要显示的HTML:
<html>
<head>
<link rel='stylesheet' type='text/css' href='default.css'/>
</head>
<body>
STA: TBM "J" Elev: 19.76<br>Desc: USGS Test Site
<div>
<table border class="boldtable">
<tr BGCOLOR="ADDAFE">
<th>STA </th>
<th>BS </th>
<th>HI </th>
<th>FS </th>
<th>ELEV </th>
<th>Desc</th>
</tr>
<p><tr>
<td> TBM "J"</td>
<td></td><td></td><td></td>
<td>19.76</td>
<td>USGS Test Site</td>
<p><tr
><td><img src="reading.png" align=center></td><td>3.14</td><td valign="middle">22.90</td>
Run Code Online (Sandbox Code Playgroud)
更新:我认为我不够清楚......在外发邮件中显示的HTML中,应该有一个图像出现在整个HTML中.我没有问题将图像附加到消息,但它没有出现在HTML中,这是我想要的地方.所以清楚地将图像附加到消息上是行不通的......(我以为会这样).
HTH ...
我同意基础64的答案.这是我使用常见NSData base 64类别的解决方案.
static NSString *EmailBody(NSString *description, UIImage *image)
{
NSString *format = @"<html>"
@"<body>"
@"<p>%@</p>"
@"<p><b><img src='data:image/png;base64,%@'></b></p>"
@"</body>"
@"</html>";
NSData *imageData = UIImagePNGRepresentation(image);
return [NSString stringWithFormat:format, description, [imageData base64EncodedString]];
}
Run Code Online (Sandbox Code Playgroud)
为了澄清事情,我会用你的例子.
<html>
<head>
<link rel='stylesheet' type='text/css' href='default.css'/>
</head>
<body>
STA: TBM "J" Elev: 19.76<br>Desc: USGS Test Site
<div>
<table border class="boldtable">
<tr BGCOLOR="ADDAFE">
<th>STA </th>
<th>BS </th>
<th>HI </th>
<th>FS </th>
<th>ELEV </th>
<th>Desc</th>
</tr>
<p><tr>
<td> TBM "J"</td>
<td></td><td></td><td></td>
<td>19.76</td>
<td>USGS Test Site</td>
<p><tr
><td><img src="data:image/png;base64,##########" align=center></td><td>3.14</td><td valign="middle">22.90</td>
Run Code Online (Sandbox Code Playgroud)
将##########替换为其基础64编码后的reading.png数据.
也许你可以使用base64编码的数据:
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA
AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO
9TXL0Y4OHwAAAABJRU5ErkJggg==" align=center alt="reading">
Run Code Online (Sandbox Code Playgroud)