user1069020
user1069020

Reputation: 31

how to add an image in the MFMailComposer message body(not as attachment)

I want to add an image in the composer such that when i open the mail composer ,the image should be there in the message body. I do not want an attachment. And i also do not want to convert the image to base64.Is there any other way???

Thank you.

Upvotes: 3

Views: 2486

Answers (3)

RajKrish
RajKrish

Reputation: 19

//To Append an attachment:

//Create a string with HTML formatting for the email body
NSMutableString *emailBody = [[NSMutableString alloc] initWithString:@"<html><body>"] ;
//Add some text to it however you want
[emailBody appendString:@"<p>Some email body text can go here</p>"];
//Pick an image to insert
//This example would come from the main bundle, but your source can be elsewhere
NSString *filePath = @"";/*you file path*/
//Convert the file into data
NSData *attachmentData = [NSData dataWithContentsOfFile:filePath];
//Create a base64 string representation of the data using NSData+Base64
NSString *base64String = [attachmentData base64EncodedString];
//Add the encoded string to the emailBody string
//Don't forget the "<b>" tags are required, the "<p>" tags are optional
[emailBody appendString:[NSString stringWithFormat:@"<p><b><img src='data:image/png;base64,%@'>  </b></p>",base64String]];
//You could repeat here with more text or images, otherwise
//close the HTML formatting
[emailBody appendString:@"</body></html>"];
NSLog(@"%@",emailBody);

//Create the mail composer window
MFMailComposeViewController *email = [[MFMailComposeViewController alloc] init];
email.mailComposeDelegate = self;
[email setSubject:@"My Inline Document"];
[email setMessageBody:emailBody isHTML:YES];

[self presentModalViewController:email animated:YES];
[email release];
[emailBody release];

Upvotes: 0

parag
parag

Reputation: 657

hello i have work using below code its work perfectly in iphone and ipad .i have attacha image file using this code. its working code.

And ios 3.0 or later directly insert image at attach in iphone

UIImage * image = [UIImage imageWithContentsOfFile:imagePath];
[composer addAttachmentData:UIImageJPEGRepresentation(itemImage, 1) mimeType:@"image/jpeg" fileName:@"MyFile.jpeg"];

Upvotes: 2

parag
parag

Reputation: 657

hello i have work using below code its work perfectly in iphone and ipad .i have attacha image file using this code. its working code.

- (void)createEmail {
//Create a string with HTML formatting for the email body
NSMutableString *emailBody = [[[NSMutableString alloc] initWithString:@"<html><body>"] retain];
//Add some text to it however you want
[emailBody appendString:@"<p>Some email body text can go here</p>"];
//Pick an image to insert
 //This example would come from the main bundle, but your source can be elsewhere
UIImage *emailImage = [UIImage imageNamed:@"myImageName.png"];
//Convert the image into data
NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(emailImage)];
//Create a base64 string representation of the data using NSData+Base64
NSString *base64String = [imageData base64EncodedString];
//Add the encoded string to the emailBody string
//Don't forget the "<b>" tags are required, the "<p>" tags are optional
[emailBody appendString:[NSString stringWithFormat:@"<p><b><img src='data:image/png;base64,%@'>  </b></p>",base64String]];
//You could repeat here with more text or images, otherwise
//close the HTML formatting
[emailBody appendString:@"</body></html>"];
NSLog(@"%@",emailBody);

//Create the mail composer window
MFMailComposeViewController *emailDialog = [[MFMailComposeViewController alloc] init];
emailDialog.mailComposeDelegate = self;
[emailDialog setSubject:@"My Inline Image Document"];
[emailDialog setMessageBody:emailBody isHTML:YES];

[self presentModalViewController:emailDialog animated:YES];
[emailDialog release];
[emailBody release];
}

Upvotes: 6

Related Questions