2

我想知道是否有一种方法可以让用户从相机胶卷中选择图像,然后将其附加到电子邮件中?

这是我现在拥有的代码:

-(IBAction) openEmail {

MFMailComposeViewController *mailComposer = [[MFMailComposeViewController alloc] init];
[mailComposer setMailComposeDelegate:self];
if ([MFMailComposeViewController canSendMail]) {
    [mailComposer setToRecipients:[NSArray arrayWithObjects:@"TPsecondary_Example@email.com", nil]];
    [mailComposer setSubject:@"Learning Trail Submission"];
    [mailComposer setMessageBody:emailbody isHTML:NO];
    [mailComposer setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];

    NSString *path = [[NSBundle mainBundle] pathForResource:@"Answer" ofType:@"plist"];
    NSData *myData = [NSData dataWithContentsOfFile:path];
    [mailComposer addAttachmentData:myData mimeType:@"application/xml" fileName:@"Answer.plist"];

    [self presentModalViewController:mailComposer animated:YES];
}
}
4

1 回答 1

1

肯定有!

在您的 .h 文件中添加这些委托并声明一个UIImage命名的 selectedImage。

<UIImagePickerControllerDelegate, UINavigationControllerDelegate>

然后在您的 .m 中,您可以添加以下内容。

链接-(IBAction)openImagePicker:(id)sender到您要启动该过程的按钮。

- (IBAction)openImagePicker:(id)sender
{

    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
    {
        UIImagePickerController *imagePicker =
        [[UIImagePickerController alloc] init];
        imagePicker.delegate = self;
        imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
        imagePicker.allowsEditing = NO;
        [self presentViewController:imagePicker animated:YES completion:nil];
    }
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    selectedImage = [info objectForKey:UIImagePickerControllerOriginalImage];
    [self dismissViewControllerAnimated:YES completion:^{
        [self openEmail];
    }];

}

-(IBAction) openEmail {

    MFMailComposeViewController *mailComposer = [[MFMailComposeViewController alloc] init];
    [mailComposer setMailComposeDelegate:self];
    if ([MFMailComposeViewController canSendMail]) {
        [mailComposer setToRecipients:[NSArray arrayWithObjects:@"TPsecondary_Example@email.com", nil]];
        [mailComposer setSubject:@"Learning Trail Submission"];
        [mailComposer setMessageBody:emailbody isHTML:NO];
        [mailComposer setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];

        NSString *path = [[NSBundle mainBundle] pathForResource:@"Answer" ofType:@"plist"];
        NSData *myData = [NSData dataWithContentsOfFile:path];
        [mailComposer addAttachmentData:myData mimeType:@"application/xml" fileName:@"Answer.plist"];
        NSData *imageData = UIImageJPEGRepresentation(selectedImage, 1.0);
        [mailComposer addAttachmentData:imageData  mimeType:@"image/jpg"   fileName:@"imageTitle"];
        [self presentModalViewController:mailComposer animated:YES];
    }
}

编辑:请注意,这是一个非常基本的示例,它不处理诸如用户选择视频而不是图像之类的事件...

于 2012-08-17T02:01:45.250 回答