nick
nick

Reputation: 2853

Automatically display UIImagePickerController on application load

Currently, my app loads a UIImagePickerController when a button is pressed. I would like to get rid of the button and have it load automatically when the view loads and then have the app close after an image is selected.

This is the method I have been using for when the button is clicked.

-(BOOL)startMediaBrowserFromViewController:(UIViewController *)controller usingDelegate:(id<UIImagePickerControllerDelegate,UINavigationControllerDelegate>)delegate
{
if(([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum] == NO)
   || (delegate == nil)
   || (controller == nil))
    return NO;
UIImagePickerController *mediaUI = [[UIImagePickerController alloc] init];
mediaUI.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeSavedPhotosAlbum];
mediaUI.allowsEditing = YES;
mediaUI.delegate = delegate;

[controller presentModalViewController:mediaUI animated: YES];
return YES;
}

I figured I could just call this method in the loadView method. I tried three ways. First, the obvious one.

[self startMediaBrowserFromViewController:self usingDelegate:self]

That didn't work, but still returned YES which I found odd.

Next, I called the method that is used when the button is clicked.

[self btnShowClicked:nil]

No luck there either. Finally, I tried one that I thought would have to work, since it would be simulating a touch on the button (if I understand it correctly)

[self.btnShow sendActionsForControlEvents:UIControlEventTouchUpInside];

Still no luck and I'm out of ideas.

Does anyone know of a way to do this? I don't think it should be that difficult.

Upvotes: 0

Views: 228

Answers (1)

Tim
Tim

Reputation: 14446

Add your code (any of the three methods you tried - the first looks fine) to your viewDidAppear method instead of in loadView. Make sure to also call [super viewDidAppear] first.

The problem relies on [controller presentModalViewController:mediaUI animated: YES]; being called after everything is set up properly.

Upvotes: 1

Related Questions