Ios 在UIImagePickerController中选择图像后MBProgressHUD

Ios 在UIImagePickerController中选择图像后MBProgressHUD,ios,objective-c,uiimagepickercontroller,mbprogresshud,Ios,Objective C,Uiimagepickercontroller,Mbprogresshud,我正在UIImagePickerController中选择一个图像。 选择图像后,我将触发另一个UIActionSheet以进行选择 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { _selected_image = info[UIImagePickerControllerEditedImage];

我正在UIImagePickerController中选择一个图像。 选择图像后,我将触发另一个
UIActionSheet
以进行选择

- (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];
}
当用户选择“共享”时,一个漫长的过程开始了。 我希望在此过程中显示MBProgressHUD。在事情开始发生之前,我无法让HUD显示进度

尝试了两件事:

- (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];
    }
}
当imagePicker仍然显示时,doSharePhotos开始运行

尝试:

- (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];
    }
}
在这种情况下,doSharePhoto内部的
MBProgressHUD
根本不存在

**编辑**:

启动HUD的代码位于
[self doSharePhoto]
中:

- (void)doSharePhoto {

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

有什么想法吗?

您的doSharePhoto方法是否在单独的线程中执行长时间运行的操作?如果没有,它会阻塞主线程,因此MBProgressHUD没有机会显示。你的
doSharePhoto
一定是这样的

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

}
编辑:问题可能在于您将HUD添加到的视图

更改
self.navigationController.view
to

self.navigationController.visibleViewController.view
所以


您需要将HUD添加到适当的视图层次以显示它。如果它位于任何其他视图的后面,它将不可见。从您的代码中,我看不到任何HUD对象,你从哪里开始显示它?你的
MBProgressHUD
相关代码是什么样子的?在doSharePhoto内部-进行了编辑…第二件事不是问题,但调度异步确实做到了。。。thanksHow你会用swift写这个吗?@return0 dispatch\u代码的异步部分对于swift也是一样的。@sleepwalkerfx谢谢,我不知道。@return0但是在swift版本中,你不会在块语法中使用插入符号(^),只要从上面的代码中删除插入符号,它就会工作。但是,当最后一个参数是块时,Swift允许您将其放在括号外,如下所示,
dispatch\u async(dispatch\u get\u global\u queue(dispatch\u queue\u PRIORITY\u LOW,0)){/*长时间运行操作*/dispatch\u async(dispatch\u get\u main\u queue(),{/*hide progresshud*/}
[MBProgressHUD hideHUDForView:self.navigationController.visibleViewController.view animated:YES];