1
votes

I have created a new app under xcode 6 and an old cold no more work. Since iOS 8 we use UIalertcontroller to show action sheet

I use it to lauch photolibrary and select a picture but when I want to dismiss the picker, my app crash and I don't know why. Below my code: UIImagePickerController *picker = [[UIImagePickerController alloc] init]; picker.delegate = self; picker.allowsEditing = YES;

UIAlertController * uiViewActionSheet=   [UIAlertController
                             alertControllerWithTitle:@"Action"
                             message:nil
                             preferredStyle:UIAlertControllerStyleActionSheet];

UIAlertAction* chooseExisting = [UIAlertAction
                            actionWithTitle:NSLocalizedString(@"CHOOSE_EXISTING",@"Choose Existing")
                            style:UIAlertActionStyleDefault
                            handler:^(UIAlertAction * action)
                            {
                                //Do some thing here
                               picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

                                [self presentViewController:picker animated:YES completion:NULL];

                                [uiViewActionSheet dismissViewControllerAnimated:YES completion:nil];

                            }];

UIAlertAction* cancel = [UIAlertAction
                         actionWithTitle:@"Cancel"
                         style:UIAlertActionStyleCancel
                         handler:^(UIAlertAction * action)
                         {
                             [uiViewActionSheet dismissViewControllerAnimated:YES completion:nil];
                         }];

[uiViewActionSheet addAction:chooseExisting];
[uiViewActionSheet addAction:cancel];
[self presentViewController:uiViewActionSheet animated:YES completion:nil];    

Below the imagepicker function :

(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { 
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
self.uImageProfile.image = chosenImage;
self.lstatutImage.text = @"save";
[picker.presentingViewController dismissViewControllerAnimated:YES completion:nil];

if(picker.sourceType == UIImagePickerControllerSourceTypeCamera)
{
    UIImageWriteToSavedPhotosAlbum(chosenImage, nil, nil, nil);
}

__block NSString* resultSaveImage = @"";
//Save image onthe server
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

    resultSaveImage = [controllerObject saveProfileImage:self.uImageProfile.image];

    dispatch_async(dispatch_get_main_queue(), ^{

        if(![resultSaveImage isEqualToString:@"\"ok\""])
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"ERROR",@"Error") message:resultSaveImage delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
            [alert show];
        }
    });
});

}

My code crash here :

[picker.presentingViewController dismissViewControllerAnimated:YES completion:nil];

Have you any idea ?

1
I just reboot the simulator and now it works... :) (1 hour lost...)Jerem

1 Answers

0
votes

You're dismissing the picker, and then calling it:

[picker.presentingViewController dismissViewControllerAnimated:YES completion:nil];

if(picker.sourceType == UIImagePickerControllerSourceTypeCamera) {...

You also don't need the [uiViewActionSheet dismissViewControllerAnimated:YES completion:nil]; lines!

Try moving the dismiss line to the end. If it still crashes, it might be that the presentingViewController is not alive anymore.