如何从iPhone库中附加邮件中的图像?

时间:2012-05-25 06:51:49

标签: iphone ios ipad

我可以从iPhone图库中检索图像并在imageView中显示它但我无法将其附加到邮件中...它只显示了一个?邮件中的图片....任何人都可以帮助我摆脱这个... 提前谢谢

http://imageshack.us/photo/my-images/826/screenshot20120525at124.png/

这是我发送邮件时获得的扫描图片....

4 个答案:

答案 0 :(得分:2)

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"]

答案 1 :(得分:1)

请尝试以下代码:

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

答案 2 :(得分:1)

你应该有这样的东西:

MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
[picker setSubject:....]
...

您所要做的就是:

NSData *data = UIImagePNGRepresentation(image);

[picker addAttachmentData:data mimeType:@"image/png" fileName:@"TheNameYouWant"];

image的类型为UIImage

更新:

这不是一个延迟问题吗?您发布的屏幕截图正是我刚刚加载图片时获得的图片。你试过等几秒钟吗??

答案 3 :(得分:1)

首先在项目中导入名为 - MessageUI.framework的导入框架。

然后导入

#import <MessageUI/MFMailComposeViewController.h>     
在viewcontroller.h中的

部分。现在添加委托

<MFMailComposeViewControllerDelegate>

现在您可以使用Mail composer视图。

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

希望这有用。

谢谢!