无法立即显示图像选取器



我有一个选项卡栏控制器,当我点击第三个选项卡栏按钮时,我会显示一个UIViewcontroller。在这个 vc 的viewWillAppear中,我展示了一个工作正常的UIImagepickerController。问题是当我打开视图时,我无法立即在屏幕上显示它。首先显示 vc,0.4-0.5 秒后显示图像选择器。因此,我想先呈现图像选择器,并在用户拍摄图像后呈现 vc。我也试图从viewDidLoadviewWillAppear打电话给选择器,但没有任何变化。

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if (imagePickerWasPresented == NO)
    {
        imagePickerWasPresented = YES;
        self.imagePicker = [[UIImagePickerController alloc] init];
        self.imagePicker.delegate = self;
        self.imagePicker.allowsEditing = YES;

        self.imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
        self.imagePicker.mediaTypes = [NSArray arrayWithObject:(NSString *)kUTTypeImage];
        [self presentViewController:self.imagePicker animated:NO completion:nil];
    }
}

我是不是叫错地方了?

我遇到了同样的问题 - 与其调用VC,然后调用UIImagePicker,不如直接调用UIImagePicker。

拍摄完照片/视频后:

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {}

您将转到此标准委托方法,从此处调用 VC。这样,您将立即转到ImagePicker,并且只有过渡,之后您才会选择对拍摄的内容执行一些不那么令人沮丧/丑陋的事情。

不,你在一个好的地方调用它,这就是iOS的做法;如果你在彼此之上呈现多个模态,一个会一个接一个地呈现,包括动画。解决您问题的解决方案是提供一个UINavigationController而不是UIViewController。将导航控制器设置为将 ViewController 作为根视图控制器,但也将图像选取器控制器推送到堆栈上。显示此导航控制器,它应该直接转到您的图像选取器控制器。否则,请尝试同时显示 uiviewcontroller 和图像选取器控制器,并将动画设置为 NO,看看是否有效。

试试这个,看看它能让你适应你的需求有多近。 当我给它一个快速测试时,它似乎按照你的要求做了。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] ;
    UIViewController * vc = [[UIViewController alloc] init];
    vc.view.backgroundColor = [UIColor whiteColor];
    UINavigationController * navigationController = [[UINavigationController alloc] init];
    [navigationController pushViewController:vc animated:NO];

    UITabBarController * tabBarController = [[UITabBarController alloc] init];
    NSArray* controllers = [NSArray arrayWithObjects:navigationController, nil];
    tabBarController.viewControllers = controllers;
    tabBarController.delegate = self;
    UIImagePickerController *imagePicker =
    [[UIImagePickerController alloc] init];
    imagePicker.delegate = self;
    imagePicker.sourceType =
    UIImagePickerControllerSourceTypePhotoLibrary;
    imagePicker.mediaTypes = [NSArray arrayWithObjects:
                          (NSString *) kUTTypeImage,
                          nil];
    imagePicker.allowsEditing = NO;
    self.window.rootViewController = tabBarController;
    [self.window makeKeyAndVisible];
    [vc presentViewController:imagePicker animated:NO completion:nil];
    return YES;
}

UIImage as a popover

图库模式:

BOOL hasGallery = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary];
UIImagePickerController* picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = hasGalleryt ? UIImagePickerControllerSourceTypePhotoLibrary :    UIImagePickerControllerSourceTypePhotoLibrary;
if (self.popoverController != nil)
{
    [self.popoverController dismissPopoverAnimated:YES];
    self.popoverController=nil;
}
self.popoverController = [[UIPopoverController alloc] initWithContentViewController:picker];
CGRect popoverRect = [self.view convertRect:[self.imageView frame]
                                   fromView:[self.imageView superview]];
popoverRect.size.width = MIN(popoverRect.size.width, 300) ;
popoverRect.origin.x = popoverRect.origin.x;
[self.popoverController
 presentPopoverFromRect:popoverRect
 inView:self.view
 permittedArrowDirections:UIPopoverArrowDirectionAny
 animated:YES];

相机模式:

BOOL hasCamera = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
UIImagePickerController* picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = hasCamera ? UIImagePickerControllerSourceTypeCamera :    UIImagePickerControllerSourceTypePhotoLibrary;
if (self.popoverController != nil)
{
    [self.popoverController dismissPopoverAnimated:YES];
    self.popoverController = nil;
}
self.popoverController = [[UIPopoverController alloc] initWithContentViewController:picker];
CGRect popoverRect = [self.view convertRect:[self.imageView frame]
                                   fromView:[self.imageView superview]];
popoverRect.size.width = MIN(popoverRect.size.width, 300) ;
popoverRect.origin.x = popoverRect.origin.x;
[self.popoverController
 presentPopoverFromRect:popoverRect
 inView:self.view
 permittedArrowDirections:UIPopoverArrowDirectionAny
 animated:YES];

记得给 .h 文件@interface代表,像这样:

@interface the UIViewController: UIViewController <UIPopoverControllerDelegate, UIImagePickerControllerDelegate>

最新更新