Reputation: 41
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
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
Reputation: 89102
Instead of using a DependencyService, try the Xamarin Media plugin.
var file = await CrossMedia.Current.PickPhotoAsync();
Upvotes: 3
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