views:

610

answers:

2

There is a part of the iPhone app that I'm developing where you can send images using the in app mail in iPhone 3.0. Selecting an image from the camera roll works perfectly, but when I try to go from the camera to the email (ie - from the UIImagePickerController to the MFMailComposeViewController), the application crashes.

This is the code for running the camera :

- (BOOL)startCameraPickerFromViewController:(UIViewController*)controller usingDelegate:(id<UIImagePickerControllerDelegate, UINavigationControllerDelegate>)delegateObject 
{
    if ( (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) || (delegateObject == nil) || (controller == nil)) 
        return NO;
    UIImagePickerController* picker = [[UIImagePickerController alloc] init];
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    picker.delegate = delegateObject;
    picker.allowsImageEditing = NO;
    [controller presentModalViewController:picker animated:YES];
    return YES;
}

And this is the code for finishing with the camera :

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
    NSLog(@"Called finish picking");
    self.imageForSending = theImage;

    //   NSData *imageData = UIImageJPEGRepresentation(image, 1);

    [[picker parentViewController] dismissModalViewControllerAnimated:YES];
    [(ChannelTwoAppDelegate *) [[UIApplication sharedApplication] delegate] recoverNavigationBar];

    [self performSelector:@selector(sendEmail) withObject:nil afterDelay:0.45];
    [picker release];
}

And this is the code for sending the mail :

- (void) sendEmail {

    [(ChannelTwoAppDelegate *) [[UIApplication sharedApplication] delegate] hideNavigationBar];
    if (![MFMailComposeViewController canSendMail])
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"שגיאה", @"") message:NSLocalizedString(@"לא ניתן לשלוח מייל ממכשיר זה", @"")
                                                       delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
        [alert show];   
        [alert release];
    }
    else
    {
        MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
        controller.mailComposeDelegate = self;
        [[controller navigationBar] setTintColor:[UIColor colorWithRed:120.0/255.0 green:0 blue:0 alpha:1.0]];
        NSData *imageData = UIImageJPEGRepresentation(imageForSending, 1);
        [controller addAttachmentData:imageData mimeType:@"image/jpg" fileName:@"storyImage.jpg"];
        [controller setSubject:@""];
        [controller setToRecipients:[NSArray arrayWithObject:@""]];
        [self presentModalViewController:controller animated:YES];
        [controller release];
    }

}

I erased the email address and the subject since it is less relevant.

The crash happens at presentModalViewController of the email. Again - this exact code work perfectly when selecting an image from the camera roll...

Help ? I've been fighting with this one for a while and could really use some new input. Thanks!

A: 

Hi to anyone watching, I found the answer to my own question. Its a timing issue between the presentation of two modal view controllers

Basically, I already knew that this was an issue, but, I hadn't put in a long enough wait. The wait was enough for selecting an image from the camera roll but not long enough for coming back from the camera.

[self performSelector:@selector(sendEmail) withObject:nil afterDelay:0.45];

changed to :

[self performSelector:@selector(sendEmail) withObject:nil afterDelay:1.0];

I also added an UIActivityIndicator to indicate that something is happening for the user.

Happy coding !

A: 

Thanks for solution, it worked!

AVEbrahimi