在UIImagePickerController中选择图像后的MBProgressHUD

时间:2014-05-07 10:36:35

标签: ios objective-c uiimagepickercontroller mbprogresshud

我在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];
    }
}

doSharePhotos在imagePicker仍在显示时开始运行。

尝试:

- (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;
...
}

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

你的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

self.navigationController.visibleViewController.view

所以,

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