ios - 在 UIImagePickerController 中选择图像后的 MBProgressHUD

标签 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;
...
}

有什么想法吗?

最佳答案

您的 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 的 View 上,

改变
self.navigationController.view

self.navigationController.visibleViewController.view

所以,

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

关于ios - 在 UIImagePickerController 中选择图像后的 MBProgressHUD,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23515427/

相关文章:

ios - 在 XCTest 用例中处理 CLLocationManager 授权请求

ios - Swift - 应用程序中的慢动作相机

objective-c - 小牛风格标记

ios - 动态加载 Objective C 代码

ios - 在 iOS 的另一个 View 中打开相机

iOS - 是否可以在将图像文件从应用程序保存到 iPhone 设备库之前重命名图像文件名?

ios - 如何快速将分钟添加到当前时间

objective-c - 在 macOS 上获取 CPU 使用百分比

自定义键盘扩展中的 iOS8 UIImagePickerController

ios - 带圆点的动画线图