Forum Xamarin.iOS

UIImagePickerController does not open on iPad

I am trying to open a photo selector dialog for uploading an image.

It works all right on iPhone simulator, but doesn’t do anything on my connected iPad. No error is thrown, the dialog just does not open, even though I can see in my console the “Shown” message.
I have read that the image picker needs to be opened in popover instead of fullscreen, which I have implemented:

UIImagePickerController imagePicker;

public void Images_Click(object sender, EventArgs e)
{
    if (imagePicker == null)
    {
        imagePicker = new UIImagePickerController();
    }                   

    imagePicker.SourceType = UIImagePickerControllerSourceType.PhotoLibrary;

    if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Pad)
    {
        imagePicker.ModalPresentationStyle = UIModalPresentationStyle.Popover;
        imagePicker.PopoverPresentationController.SourceView = View;
    }
    else
    {
        imagePicker.ModalPresentationStyle = UIModalPresentationStyle.FullScreen;
    }

    imagePicker.Canceled += (object sender1, EventArgs e1) =>
    {
        imagePicker.DismissViewController(true, null);
    };

    imagePicker.FinishedPickingMedia += async (object sender1, UIImagePickerMediaPickedEventArgs e1) =>
    {
        imagePicker.DismissViewController(true, null);
        await UploadFile(e1.ImageUrl.Path);
    };

    PresentViewController(imagePicker, true, () => { Console.WriteLine("Shown"); });
}

Checking these values gives the same results on the simulator and on the iPad:

Console.WriteLine("IsSourceTypeAvailable: " + UIImagePickerController.IsSourceTypeAvailable(UIImagePickerControllerSourceType.PhotoLibrary) + " AvailableMediaTypes: " + UIImagePickerController.AvailableMediaTypes(UIImagePickerControllerSourceType.PhotoLibrary));

// IsSourceTypeAvailable: True AvailableMediaTypes: System.String[]

Any idea?

Best Answer

  • fodorbalintfodorbalint ✭✭
    Accepted Answer

    After more research I found the simple solution:

    Instead of

    imagePicker.PopoverPresentationController.SourceView = View;
    

    you should write

    imagePicker.PopoverPresentationController.SourceView = ImageUploadButton;
    

    where ImageUploadButton is the button on which the user clicked.

    Obviously you wouldn't know that if you have never seen a popover in real life. But it is a small window that emanates from the middle of the control.

Answers

  • fodorbalintfodorbalint Member ✭✭
    Accepted Answer

    After more research I found the simple solution:

    Instead of

    imagePicker.PopoverPresentationController.SourceView = View;
    

    you should write

    imagePicker.PopoverPresentationController.SourceView = ImageUploadButton;
    

    where ImageUploadButton is the button on which the user clicked.

    Obviously you wouldn't know that if you have never seen a popover in real life. But it is a small window that emanates from the middle of the control.

Sign In or Register to comment.