我可以从 iPhone 库中检索图像并将其显示在 imageView 中,但我无法将其附加到邮件中......它只显示一个?邮件中的图片....任何人都可以帮我解决这个问题...谢谢
http://imageshack.us/photo/my-images/826/screenshot20120525at124.png/
这是我在发送邮件时收到的屏幕截图....
我可以从 iPhone 库中检索图像并将其显示在 imageView 中,但我无法将其附加到邮件中......它只显示一个?邮件中的图片....任何人都可以帮我解决这个问题...谢谢
http://imageshack.us/photo/my-images/826/screenshot20120525at124.png/
这是我在发送邮件时收到的屏幕截图....
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:@"My Image Is Attached"];
// other mail settings here
//Attachment imageData1 is NSData of your image.
[picker addAttachmentData:imageData1 mimeType:@"image/png" fileName:@"foo.png"]
请尝试以下代码:
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:@"Hello"];
// Set up recipients
NSArray *toRecipients = [NSArray arrayWithObject:@"first@example.com"];
NSArray *ccRecipients = [NSArray arrayWithObjects:@"second@example.com", @"third@example.com", nil];
NSArray *bccRecipients = [NSArray arrayWithObject:@"fourth@example.com"];
[picker setToRecipients:toRecipients];
[picker setCcRecipients:ccRecipients];
[picker setBccRecipients:bccRecipients];
// Attach an image to the email
NSString *path = [[NSBundle mainBundle] pathForResource:@"rainy" ofType:@"png"];
NSData *myData = [NSData dataWithContentsOfFile:path];
[picker addAttachmentData:myData mimeType:@"image/png" fileName:@"rainy"];
// Fill out the email body text
NSString *emailBody = @"It is raining";
[picker setMessageBody:emailBody isHTML:NO];
[self presentModalViewController:picker animated:YES];
[picker release];
你应该有这样的东西:
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
[picker setSubject:....]
...
你所要做的就是:
NSData *data = UIImagePNGRepresentation(image);
[picker addAttachmentData:data mimeType:@"image/png" fileName:@"TheNameYouWant"];
image
类型在哪里UIImage
。
更新:
不能只是延迟的问题吗?您发布的屏幕截图正是我在加载图像时得到的图像。您是否尝试过等待几秒钟...?
首先在您的项目中导入名为 - MessageUI.framework 的框架。
然后导入
#import <MessageUI/MFMailComposeViewController.h>
在您的 viewcontroller.h 部分。现在添加委托
<MFMailComposeViewControllerDelegate>
现在您可以使用邮件编辑器视图了。
-(IBAction)showMail:(id)sender //this is your UIButton action
{
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:@"write your subject here"];
UIImage *image = [UIImage imageNamed:@"anyImage.png"];
//convert UIImage to NSData to add it as attachment
NSData *imageData = UIImagePNGRepresentation(image);
//this is how to attach any data in mail, remember mimeType will be different
//for other data type attachment.
[picker addAttachmentData:imageData mimeType:@"image/png" fileName:@"image.png"];
//showing MFMailComposerView here
[self presentModalViewController:picker animated:YES];
}
完成工作后,您可以关闭 MFMailComposerView,如下所示:-
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
if(result == MFMailComposeResultCancelled)
NSLog(@"Mail has cancelled");
if(result == MFMailComposeResultSaved)
NSLog(@"Mail has saved");
[self dismissModalViewControllerAnimated:YES];
}
希望这会有所帮助。
谢谢你!