如何在MFMailComposeViewController的MailComposer Sheet中添加UIImage

rkb*_*rkb 53 iphone attachment ios mfmailcomposeviewcontroller

我想在一个UIImage组合表中插入一个s MFMailComposerViewController.

请注意我不想附加它们,但我想使用HTML代码将它们放在一个表格中,这将是电子邮件正文的一部分.

Mik*_*ike 64

再回来一个新的答案.我仍然留下我以前的代码,因为我仍然不相信没有办法利用它.我会自己坚持下去.以下代码可行.Mustafa建议对图像进行base64编码,并表示他们只将Apple用于Apple,但事实并非如此.Base64编码现在可以与大多数邮件客户端一起使用(IE以前不支持它,但现在它支持最大到一定大小的图像,但我不确定大小是多少).问题是像Gmail这样的邮件客户端会删除你的图像数据,但是有一个简单的解决方法......只需在<b> and </b>标签周围放置标签<img ...>就可以防止它被剥离.为了将图像添加到您的电子邮件中,您需要在项目中使用base64编码器.有几个(虽然大多数是C),但是我找到的最简单的ObjC 被Matt Gallagher称为NSData + Base64(因为它给我带来了问题,我把它复制后取出了"+").将.h和.m文件复制到项目中,并确保#import .h文件,您打算在其中使用它.那么像这样的代码会将图像放入您的电子邮件正文...

- (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];
}
Run Code Online (Sandbox Code Playgroud)

我在iPhone上测试了这个,并在Yahoo,我的个人网站和我的MobileMe上向我自己发送了可爱的图像嵌入式电子邮件.我没有Gmail帐户,但雅虎工作得很好,我发现的每个来源都说大胆的标签就是你需要的才能让它发挥作用.希望这有助于所有人!

  • 这是关于此事的最新消息.使用Gmail,Yahoo或Hotmail时,这种方法对我无效.不显示嵌入式基础64编码图像.但是,Map.app正在显示嵌入式图像(Mac和iOS),但它还不够好. (11认同)
  • <b> </ b>技巧似乎不再起作用.无论我做什么,Gmail都不会显示图像.桌面客户端工作正常. (6认同)
  • `[[[NSMutableString alloc] initWithString:@"<html> <body>"]保留]`:alloc已经保留了,所以你保留了太多 (3认同)

Ram*_*min 11

有两种方法可以执行此操作,具体取决于图像的存储位置:

如果图像在服务器上,则只需包含HTML <img>标记,并将源URL设置为远程图像.预览邮件消息的用户在合成期间显示图像,接收者在打开消息时看到它(除非他们已禁用默认图像加载).

如果图像在手机上,您可以将它们包含为"内嵌"图像.这有两个步骤.首先,您必须附加要用作多部分MIME附件的所有图像,并且需要为它们分配"内容ID"(也称为cid),文件名和Content-Disposition设置为inline.在您的HTML邮件正文中,您可以像这样引用它们:

<img src="cid:{messageid}/image.png" alt="My image" />
Run Code Online (Sandbox Code Playgroud)

坏消息是,标准的iPhone邮件编辑器机制不允许将这些额外的数据添加到附件中.第二件事是将电子邮件标记为具有"替代"MIME内容类型.再次,邮件编辑器不允许你这样做.

解决这个问题的方法是自己撰写邮件,然后通过SMTP直接将邮件发送到邮件服务器,或让服务器代理通过SMTP中继为您完成.如果您决定采用这种方式,可能需要查看Google代码或AuthSMTP等服务上的skpsmtpmessage.

但是,一旦用户收到此消息,他们就会看到包含所有内嵌图像的自包含HTML消息.但是设置起来很麻烦.第一种方法(将图像放在服务器上)是迄今为止更容易的方法.