如何在 MFMailComposeViewController 的 MailComposer Sheet 中添加 UIImage [英] How to add a UIImage in MailComposer Sheet of MFMailComposeViewController

查看:22
本文介绍了如何在 MFMailComposeViewController 的 MailComposer Sheet 中添加 UIImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 MFMailComposerViewController 的撰写表中插入一个 UIImages.

I want to insert a UIImages inside the compose sheet of an MFMailComposerViewController.

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

Please note I don't want to attach them, but I want to place them in a table using HTML code which will be the part of the email body.

推荐答案

再次返回一个新的答案.我仍然保留我以前的代码,因为我仍然不相信没有办法使用它.我自己会坚持的.以下代码确实有效.Mustafa 建议对图像进行 base64 编码,并表示它们仅适用于 Apple 对 Apple,但实际上并非如此.Base64 编码现在确实适用于大多数邮件客户端(IE 以前不支持它,但现在它支持特定大小的图像,尽管我不确定大小是多少).问题是像 Gmail 这样的邮件客户端会删除您的图像数据,但有一个简单的解决方法......只需将 <b>和 <img ...> 标签周围的 </b> 标签是您需要做的一切,以防止它被剥离.为了将图像放入您的电子邮件,您需要在您的项目中加入一个 base64 编码器.有几个(虽然主要是 C),但我发现的最简单的 ObjC 被称为 NSData+Base64 by Matt Gallagher(我在复制它后去掉了名称中的+",因为它给了我问题).将 .h 和 .m 文件复制到您的项目中,并确保在您计划使用它的地方 #import .h 文件.然后像这样的代码会将图像放入您的电子邮件正文...

Back again with a new answer. I'm still leaving my previous code up though, because I'm still not convinced that there's not a way to make use of it. I'll keep at it myself. The following code DOES work. Mustafa suggests base64 encoding the images, and says that they only work Apple to Apple, but that's not actually true. Base64 encoding does work with most mail clients now (IE previously didn't support it, but now it is supported for images up to a certain size, though I'm not sure exactly what the size is). The problem is that mail clients like Gmail would strip out your image data, but there's a simple workaround for that... just putting <b> and </b> tags around your <img ...> tag is all you need to do to keep it from getting stripped out. In order to get an image into your email, you need to get a base64 encoder into your project. There are several out there (mostly C though), but the simplest ObjC one I found was called NSData+Base64 by Matt Gallagher (I took the "+" out of the name after copying it in because it gave me problems). Copy the .h and .m files into your project and be sure to #import the .h file where you plan on using it. Then code like this will get an image into your email body...

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

我已经在 iPhone 上对此进行了测试,并在 Yahoo、我的个人网站和我的 MobileMe 上向自​​己发送了嵌入可爱图片的电子邮件.我没有 Gmail 帐户,但 Yahoo 运行良好,我发现的每个来源都说粗体标签是让它工作所需的全部内容.希望这对大家有帮助!

I've tested this on the iPhone and sent lovely image embedded emails to myself on Yahoo, my personal website, and my MobileMe. I don't have a Gmail account, but the Yahoo worked perfectly, and every source I've found says that the bold-tags are all you need to make it work. Hope this helps all!

这篇关于如何在 MFMailComposeViewController 的 MailComposer Sheet 中添加 UIImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆