0

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!

3 Answers3

3

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

var file = await CrossMedia.Current.PickPhotoAsync();
Jason
  • 86,222
  • 15
  • 131
  • 146
  • How would I display the picker using the Xamarin Media plugin? – Rick Najjar Apr 24 '16 at 23:35
  • just like I showed - the PickPhotoAsync() method will display the picker (on any platform) and return a MediaFile object with the selected image. You can also use it to take photos with the camera instead, using TakePhotoAsync(). Samples and source here: https://github.com/jamesmontemagno/Xamarin.Plugins/tree/master/Media – Jason Apr 24 '16 at 23:41
0

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);
Iain Smith
  • 9,230
  • 4
  • 50
  • 61
0

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;
tequila slammer
  • 2,821
  • 1
  • 18
  • 25