如何在MFMailComposer邮件正文中添加图像(不作为附件)

时间:2011-12-29 09:15:10

标签: html objective-c ios-simulator

我想在作曲家中添加一个图像,这样当我打开邮件编辑器时,图像应该在邮件正文中。 我不想要附件。 我也不想将图像转换为base64。还有其他任何方式???

谢谢。

3 个答案:

答案 0 :(得分:6)

你好,我使用下面的代码工作,它在iphone和ipad中完美地工作.i使用此代码的attacha图像文件。它的工作代码。

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

答案 1 :(得分:2)

你好,我使用下面的代码工作,它在iphone和ipad中完美地工作.i使用此代码的attacha图像文件。它的工作代码。

ios 3.0或更高版本直接在iphone中附加图像

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

答案 2 :(得分:0)

//附加附件:

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