rihekopo
rihekopo

Reputation: 3350

Can't present imagepicker immediately

I have a Tab bar controller, when I tap the third tab bar button I present a UIViewcontroller. In the viewWillAppear of this vc I'm presenting a UIImagepickerController that works fine. The problem is I can't display it on the screen immediately when I open the view. First the vc shows up and after 0.4-0.5 sec the image picker. So I would like to present the image picker first and present the vc after the user took an image. I tried to call the picker from viewDidLoad and viewWillAppear too, but nothing changed.

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

Am I calling it in a wrong place?

Upvotes: 0

Views: 262

Answers (4)

Luis Mejías
Luis Mejías

Reputation: 301

UIImage as a PopOver

Gallery mode:

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];

Camera mode:

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];

Remember to give to the .h File @interface the delegates, like this:

@interface the UIViewController: UIViewController <UIPopoverControllerDelegate, UIImagePickerControllerDelegate>

Upvotes: 1

trdavidson
trdavidson

Reputation: 1051

I had the same problem - instead of calling the VC and then the UIImagePicker, call the UIImagePicker directly.

When you are done taking a picture/video:

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

you will go to this standard delegate method, call the VC from here. This way you will immediately go to the ImagePicker and only have a transition would you choose to do something with the taken content afterwards which is less frustrating/ugly.

Upvotes: 2

myte
myte

Reputation: 877

try this to see how close that gets you then adapt to your needs. when i gave it a quick test it seemed to do what you were asking.

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

}

Upvotes: 1

Alex
Alex

Reputation: 3971

No, your calling it in an okay place, that's just how iOS does it; if you present multiple modals on top of each other, one gets presented after the other, including the animation. A solution that would work for your problem is to present a UINavigationController instead of your UIViewController. Set the navigation controller up to have ViewController as the root viewcontroller, but also push your imagepickercontroller onto the stack. Present this navigationcontroller and it should go right to your imagepickercontroller. Otherwise, try presenting both uiviewcontroller and imagepickercontroller with animation set to NO and see if that works.

Upvotes: 1

Related Questions