如何让这张图片显示在我的电子邮件正文中?

时间:2012-04-24 18:48:35

标签: objective-c ios

我有这个代码,它应该将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];

这是要显示的HTML:

<html>
<head>
<link rel='stylesheet' type='text/css' href='default.css'/>
</head>
 <body>
 STA:&nbsp;TBM "J" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Elev:&nbsp;19.76<br>Desc:&nbsp;USGS Test Site
 <div>
 <table border class="boldtable">
 <tr BGCOLOR="ADDAFE">
 <th>STA&nbsp;</th>
 <th>BS&nbsp;</th>
 <th>HI&nbsp;</th>
 <th>FS&nbsp;</th>
 <th>ELEV&nbsp;</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>

更新:我认为我不够清楚......在外发邮件中显示的HTML中,应该有一个图像出现在整个HTML中。我没有问题将图像附加到消息,但它没有出现在HTML中,这是我想要的地方。所以清楚地将图像附加到消息上是行不通的......(我以为会这样)。

... HTH

4 个答案:

答案 0 :(得分:8)

我同意基础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]];
}

为了澄清事情,我会用你的例子。

<html>
<head>
<link rel='stylesheet' type='text/css' href='default.css'/>
</head>
 <body>
 STA:&nbsp;TBM "J" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Elev:&nbsp;19.76<br>Desc:&nbsp;USGS Test Site
 <div>
 <table border class="boldtable">
 <tr BGCOLOR="ADDAFE">
 <th>STA&nbsp;</th>
 <th>BS&nbsp;</th>
 <th>HI&nbsp;</th>
 <th>FS&nbsp;</th>
 <th>ELEV&nbsp;</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>

其中##########在基础64编码后被reading.png的数据替换。

答案 1 :(得分:5)

也许您可以使用base64中编码的数据:

<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA
AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO
9TXL0Y4OHwAAAABJRU5ErkJggg==" align=center alt="reading">

答案 2 :(得分:3)

对于电子邮件中的图片加载,图片标记必须包含完整的网址,图片应位于某个服务器上以便加载:

<img src="http://www.mydomain.com/reading.png" align=center>

答案 3 :(得分:0)

您必须输入NSString stringWithFormat,并将.png插入NSString。我在Xcode 3中尝试了你的代码,完成了我的操作,并且它有效。希望它有所帮助!