iosobjective-cuiimagepickercontrollermbprogresshud

MBProgressHUD after selecting image in UIImagePickerController


I'm selecting an image in UIImagePickerController. After the image was selected I'm firing another UIActionSheet to select from

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    _selected_image = info[UIImagePickerControllerEditedImage];
    UIActionSheet *popup = [[UIActionSheet alloc] initWithTitle:@"" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:
                            @"Share this photo",
                            nil];
    popup.tag = 2;
    [popup showInView:[UIApplication sharedApplication].keyWindow];
}

When the user selects "Share" a long process is starting. I want the MBProgressHUD to show during the process. I can't get progress HUD to show before stuff are starting to happen.

Tried two things:

- (void)actionSheet:(UIActionSheet *)popup clickedButtonAtIndex:(NSInteger)buttonIndex {

    if (popup.tag == 1)
    {
    ...
    }
    else if (popup.tag == 2)
    {
        if (buttonIndex == 1)  //cancel
        {
            _selected_image = nil;
        }
        else if (buttonIndex == 0)
        {
            [imagePicker dismissViewControllerAnimated:YES completion:NULL];
            [self doSharePhoto];
            return;
        }
        [imagePicker dismissViewControllerAnimated:YES completion:NULL];
    }
}

doSharePhotos start running when the imagePicker is still showing.

Tried:

- (void)actionSheet:(UIActionSheet *)popup clickedButtonAtIndex:(NSInteger)buttonIndex {

    if (popup.tag == 1)
    {

    }
    else if (popup.tag == 2)
    {
        if (buttonIndex == 1)  //cancel
        {
            _selected_image = nil;
        }
        else if (buttonIndex == 0)
        {

            [imagePicker dismissViewControllerAnimated:YES completion:^{
                [self doSharePhoto];
            }];
            return;
        }
        [imagePicker dismissViewControllerAnimated:YES completion:NULL];
    }
}

and in this case the MBProgressHUD inside doSharePhoto doesn't apprear at all

** EDIT **:

The code that starts the HUD is in [self doSharePhoto]:

- (void)doSharePhoto {

    MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
    HUD.labelText = @"Please wait...";
    HUD.delegate = self;
...
}

Any thoughts?


Solution

  • Does your doSharePhoto method perform the long running operation inside a seperate thread? If not it's blocking the main thread so , MBProgressHUD doesn't get a chance to show. Your doSharePhoto must look like this,

    - (void)doSharePhoto {
    
        [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
        dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
    
            // Do the long running operation here... 
    
            dispatch_async(dispatch_get_main_queue(), ^{
                [MBProgressHUD hideHUDForView:self.navigationController.view animated:YES];
            });
        });
    
    }
    

    EDIT: Problem might be with the view you add your HUD to,

    Change
    self.navigationController.view to

    self.navigationController.visibleViewController.view
    

    So,

    [MBProgressHUD hideHUDForView:self.navigationController.visibleViewController.view animated:YES];