我正在 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/