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

I have this code that should attach an HTML file to an email, and also display the contents of the HTML file in the body of the message. There is a link in the HTML file (read.png) that appears on every line. However, it does not appear in the body of the message. What do I need to do to display it?

if ([MFMailComposeViewController canSendMail]) { MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init]; mailer.mailComposeDelegate = self; // set delegate to notify us what 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 that should 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 that appears in the outgoing message, there should be an image that appears in all of the HTML. I have no problem attaching an image to a post, but it does NOT appear in HTML where I want it. So clearly attaching the image to the message does not work ... (I thought it would be).

NTN ...

+7
source share
4 answers

I agree with the answer of base 64. Here is my solution using the common base base of NSData 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]]; } 

To clarify the situation, I will 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 .png read data after encoding base 64.

+8
source

Perhaps you can use base64 encoded data:

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

To upload an image in electronic form, the image tag must have the full URL in it, and the image must be located on the server somewhere so that it can be downloaded:

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

+3
source

You need to enter the string NSString stringWithFormat and insert .png into NSString. I tried your code in Xcode 3, did my orders, and it worked. Hope this helps!

0
source

All Articles