2010-11-29 58 views
30

我想通过从图像选取器中选取文件(图像)作为附件发送邮件。 我可以在iOS Objective-C中附加和发送文件(特别是图像)的适当方式是什么?作为附件发送文件在目标c

+0

这个链接回答了这个问题: HTTP: //stackoverflow.com/a/4302 449/1886229 – Guy 2015-05-18 17:30:45

回答

78

使用下面

-(void)displayComposerSheet 
{ 
    MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init]; 
    picker.mailComposeDelegate = self; 
    [picker setSubject:@"Check out this image!"]; 

    // Set up recipients 
    // NSArray *toRecipients = [NSArray arrayWithObject:@"[email protected]"]; 
    // NSArray *ccRecipients = [NSArray arrayWithObjects:@"[email protected]", @"[email protected]", nil]; 
    // NSArray *bccRecipients = [NSArray arrayWithObject:@"[email protected]"]; 

    // [picker setToRecipients:toRecipients]; 
    // [picker setCcRecipients:ccRecipients]; 
    // [picker setBccRecipients:bccRecipients]; 

    // Attach an image to the email 
    UIImage *coolImage = ...; 
    NSData *myData = UIImagePNGRepresentation(coolImage); 
    [picker addAttachmentData:myData mimeType:@"image/png" fileName:@"coolImage.png"]; 

    // Fill out the email body text 
    NSString *emailBody = @"My cool image is attached"; 
    [picker setMessageBody:emailBody isHTML:NO]; 
    [self presentModalViewController:picker animated:YES]; 

    [picker release]; 
} 

的方法和实现委托方法

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error 
{ 
    // Notifies users about errors associated with the interface 
    switch (result) 
    { 
     case MFMailComposeResultCancelled: 
      NSLog(@"Result: canceled"); 
      break; 
     case MFMailComposeResultSaved: 
      NSLog(@"Result: saved"); 
      break; 
     case MFMailComposeResultSent: 
      NSLog(@"Result: sent"); 
      break; 
     case MFMailComposeResultFailed: 
      NSLog(@"Result: failed"); 
      break; 
     default: 
      NSLog(@"Result: not sent"); 
      break; 
    } 
    [self dismissModalViewControllerAnimated:YES]; 
} 

而在你的接口文件

#import <MessageUI/MFMailComposeViewController.h> 
... 

@interface ... : ... <MFMailComposeViewControllerDelegate> 
+0

非常感谢:) Rog – sandy 2010-11-29 11:50:03

相关问题