Search code examples
iosobjective-cuiimagepickercontrollermfmailcomposeviewcontroller

Email a picture from photo library


I want to compose an email with picture taken from camera or photo library. But i am unable to open Mail composer picker.

Here is my code:

-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info
{
    [self dismissModalViewControllerAnimated:YES];
    UIImage* image_type=[info objectForKey:@"UIImagePickerControllerOriginalImage"];
    dataImage = UIImagePNGRepresentation(image_type);

    if([MFMailComposeViewController canSendMail])
    {
        MFMailComposeViewController *mailCont = [[MFMailComposeViewController alloc] init];
        mailCont.delegate=self;
        mailCont.mailComposeDelegate = self;        // Required to invoke mailComposeController when send
        [mailCont setSubject:@""];
        [mailCont setToRecipients:[NSArray arrayWithObject:@""]];
        [mailCont addAttachmentData:dataImage mimeType:@"image/png" fileName:@"sender_image.png"];
        [mailCont setMessageBody:@"" isHTML:NO];

        [self presentModalViewController:mailCont animated:YES];
    }
}

Now the mail picker is not opening. The warning is :

Warning: Attempt to present <MFMailComposeViewController: 0xa26b070> on <UINavigationController: 0xa22e6d0> while a presentation is in progress!

How can i handle this.


Solution

  • Your problem is that you are first dismissing the UIImagePicker and then immediately trying to display another view as modal view. This has to be done after the dismissal finished. Try this instead:

    -(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info
    {
        [self dismissViewControllerAnimated:YES
                                 completion:^{
                                     UIImage* image_type=[info objectForKey:@"UIImagePickerControllerOriginalImage"];
                                     dataImage = UIImagePNGRepresentation(image_type);
    
                                     if([MFMailComposeViewController canSendMail])
                                     {
                                         MFMailComposeViewController *mailCont = [[MFMailComposeViewController alloc] init];
                                         mailCont.delegate=self;
                                         mailCont.mailComposeDelegate = self;        // Required to invoke mailComposeController when send
                                         [mailCont setSubject:@""];
                                         [mailCont setToRecipients:[NSArray arrayWithObject:@""]];
                                         [mailCont addAttachmentData:dataImage mimeType:@"image/png" fileName:@"sender_image.png"];
                                         [mailCont setMessageBody:@"" isHTML:NO];
    
                                         [self presentModalViewController:mailCont animated:YES];
                                     }
                                 }];
    }
    

    Hope it helps!