How to get this image to display in the body of my email?

ios, objective-c

Solution

I agree with the base 64 answer. Here was my solution using a common NSData base 64 category.

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]];
}

Just to clarify things, I'll use your example.

<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>

Where ########## is replaced with the data of reading.png after it's been base 64 encoded.

Problem

I have this code, which is supposed to attach a HTML file to the email, and also display the contents of the HTML file in the body of the email. There is an image (reading.png) referenced in the HTML file that get's displayed every-other line. However, it is NOT being displayed in the body of the email. What do I have to do to get it to display? ``` 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]; ``` This is the HTML which is to be displayed: ``` <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> ``` UPDATE: I don't think I was clear enough... In the HTML which is displayed in the outgoing message, there should be an image that appears throughout the HTML. I have no problem attaching the image to the message, but it does NOT appear in the HTML, which is where I want it. So clearly attaching the image to the message is not working... (I thought it would). HTH...

Original source