Rick Najjar
Rick Najjar

Reputation: 41

ImagePicker Not Showing In Xamarin

I have an image picker in my app that opens up the users photo album (iOS) so they can select an image. I am using xamarin cross platform so I have a dependency service leading to this chunk of code. I got all the correct code after that but the issue is that the image picker doesn't display the first one or two times and only starts working after the user refreshes the entire app. Here is the following code that I am using to create the picker:

        imagePicker = new UIImagePickerController ();
        imagePicker.SourceType = UIImagePickerControllerSourceType.PhotoLibrary;
        imagePicker.MediaTypes = UIImagePickerController.AvailableMediaTypes (UIImagePickerControllerSourceType.PhotoLibrary);
        imagePicker.FinishedPickingMedia += Handle_FinishedPickingMedia;
        imagePicker.Canceled += Handle_Canceled;
        //Show the picker
        UIApplication.SharedApplication.KeyWindow.RootViewController.PresentModalViewController (imagePicker, true);

Note that I have tried NavigationController.PresentModalViewController but just end up with a null error. I also tried just "PresentViewController" but no help there. Also, is there any different way to display the image picker? Any help would be greatly appreciated!

Upvotes: 0

Views: 1459

Answers (3)

tequila slammer
tequila slammer

Reputation: 2881

It looks like you haven't set the ModalPresentationStyle property. I also noticed the missing action handler paramter in your PresentModalViewController method.

imagePicker.ModalPresentationStyle = UIModalPresentationStyle.Popover;

Upvotes: 0

Jason
Jason

Reputation: 89102

Instead of using a DependencyService, try the Xamarin Media plugin.

var file = await CrossMedia.Current.PickPhotoAsync();

Upvotes: 3

Iain Smith
Iain Smith

Reputation: 9703

PresentModalViewController is deprecated in 6.0 https://developer.xamarin.com/api/member/UIKit.UIViewController.PresentModalViewController/p/UIKit.UIViewController/System.Boolean/

Try Changing this line :

UIApplication.SharedApplication.KeyWindow.RootViewController.PresentModalViewController (imagePicker, true);

to this:

UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController (imagePicker, true, null);

Upvotes: 0

Related Questions