通过电子邮件发送使用imagePickerController拍摄的照片

时间:2015-08-11 03:43:57

标签: ios objective-c uiviewcontroller uiimagepickercontroller mfmailcomposeviewcontroller

此应用程序在viewWillAppear方法中加载设备的相机:

- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];

if (self.imageView.image == nil) {
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
    imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
    imagePickerController.modalPresentationStyle = UIModalPresentationCurrentContext;
    imagePickerController.delegate = self;
    [self presentViewController:imagePickerController animated:YES completion:nil];
}
else {   }
}

实施委托方法:

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

[self dismissViewControllerAnimated:YES completion:nil];

// Pass the image to email composer after dismissing the camera. Delay allowed for cameraVC to dismiss.
[self performSelector:@selector(composeEmail) withObject:image afterDelay:1.0];
}

- (void) imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[[picker parentViewController] dismissViewControllerAnimated:YES completion:nil];
}

当拍摄照片并选择默认的“使用照片”按钮时,我想关闭相机ViewController并加载使用此方法的emailComposer View Controller:

- (void) composeEmail: (UIImage *)image {

NSString *bodyHeader = @"Here are you directions:";
NSString *mailBody = [NSString stringWithFormat:@"%@\n%@", bodyHeader, googleMapsURL];

MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;

[picker setSubject:@"Google Maps Directions"];
[picker setMessageBody:mailBody isHTML:NO];
[picker setToRecipients:@[@"john.doe@gmail.com"]];
[picker setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];

// Create NSData object as PNG image data from camera image
NSData *data = UIImagePNGRepresentation(image);

// Attach image data to the email
// 'DestinationImage.png' is file name that will be attached to the email
[picker addAttachmentData:data mimeType:@"image/png" fileName:@"DestinationImage"];

[self presentViewController:picker animated:YES completion:nil];
}

(我在这里省略了一些MessageUI的详细信息,但我已经测试了它,我知道它正在运行)

拍摄的图像应传递给emailComposer并附在电子邮件中。当我在我的设备上构建它并点击“使用照片”按钮时会抛出错误。错误消息指出“尝试在ViewController上显示MFMailCompseViewController,其视图不在窗口层次结构中!”我只使用一个ViewController,VC包含一个图像视图。

任何人都可以帮我解雇相机并加载电子邮件作曲家吗? 非常感谢!

2 个答案:

答案 0 :(得分:0)

要显示视图控制器,请使用以下命令:

[[[[[UIApplication sharedApplication] delegate] window] rootViewController] presentViewController: picker
                                                                                                 animated:YES
                                                                                               completion:nil];

您还可以查看此stackoverflow's question以进一步了解

答案 1 :(得分:0)

首先关闭ImagePicker,然后显示你的mailcomposer将会正常工作。我解雇了那个不起作用的父母[yourpicker dismissViewControllerAnimated:YES completion:nil];

 - (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

[yourpicker dismissViewControllerAnimated:YES completion:nil];

// Pass the image to email composer after dismissing the camera. Delay allowed for cameraVC to dismiss.

[self performSelector:@selector(composeEmail) withObject:image afterDelay:1.0];
 }