0

This application loads the device's camera in viewWillAppear method:

- (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 {   }
}

Delegate methods are implemented:

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

When a photo is taken and I choose the default "Use Photo" button I want to dismiss the camera ViewController and load the emailComposer View Controller, which uses this method:

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

(I have left out some of the MessageUI details here but I have tested it and I know it is working)

The image taken should be passed to the emailComposer and attached as an email. When I build this on my device and tap the "Use Photo" button an error is thrown. The error message states "Attempt to present MFMailCompseViewController on ViewController whose view is not in the window hierarchy!" I am using only one ViewController and that VC contains one Image View.

Can anyone help me dismiss the camera and load the email composer? Much appreciated!

Michael Castro
  • 194
  • 2
  • 12

2 Answers2

0

To present view controller, use following :

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

You can also look into this stackoverflow's question for further understanding

Community
  • 1
  • 1
Munahil
  • 2,256
  • 1
  • 14
  • 21
0

First Dismiss the ImagePicker and then present ur mailcomposer will work then.U r dismissing the parent thta's y its not working [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];
 }
Mukesh
  • 3,564
  • 1
  • 12
  • 30