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

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

问题描述

我想在 MFMailComposerViewController 的撰写表中插入 UIImage

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,但事实并非如此。 Base64编码现在可以与大多数邮件客户端一起使用(IE以前不支持它,但现在它支持最大到一定大小的图像,但我不确定它的大小是多少)。问题是像Gmail这样的邮件客户端会删除你的图像数据,但是有一个简单的解决方法......只需要输入< b>和您的< img ...> 标记周围的标记就是您需要做的一切,以防止它被剥离出。为了将图像添加到您的电子邮件中,您需要在项目中添加base64编码器。有几个(虽然大多数是C),但是我找到的最简单的ObjC被Matt Gallagher称为 NSData + Base64 (我把它复制后用名字中的+因为它给了我问题)。将.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上测试了这个并向我自己发送了可爱的图像嵌入式电子邮件在雅虎,我的个人网站和我的MobileMe上。我没有Gmail帐户,但雅虎工作得很好,而且我发现的每个来源都说大胆的标签就是你需要的才能让它发挥作用。希望这对所有人都有帮助!

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天全站免登陆