Choose a Photo From the Gallery

PDF for offline use
Sample Code:
Related SDKs:

Let us know how you feel about this

Translation Quality


0/250

This recipe shows how to let the user choose a photo from their gallery.

iOS Recipe

Applications can ask the user to choose a photo (or video) from their Photo Albums using UIImagePickerController. The sample code screens look like this:

To implement the photo picker:

  1. Create the image picker control:
imagePicker = new UIImagePickerController ();
  1. Set the source and media type:
imagePicker.SourceType = UIImagePickerControllerSourceType.PhotoLibrary;
imagePicker.MediaTypes = UIImagePickerController.AvailableMediaTypes (UIImagePickerControllerSourceType.PhotoLibrary);
  1. Assign delegate handler (to be implemented in steps 5 and 6):
imagePicker.FinishedPickingMedia += Handle_FinishedPickingMedia;
imagePicker.Canceled += Handle_Canceled;
  1. Display the controller:
NavigationController.PresentModalViewController(imagePicker, true);
  1. Implement the handler method that checks the kind of file the user selected, and if it is an image displays it:
protected void Handle_FinishedPickingMedia (object sender, UIImagePickerMediaPickedEventArgs e)
{
    // determine what was selected, video or image
    bool isImage = false;
    switch(e.Info[UIImagePickerController.MediaType].ToString()) {
        case "public.image":
            Console.WriteLine("Image selected");
            isImage = true;
            break;
        case "public.video":
            Console.WriteLine("Video selected");
            break;
    }

    // get common info (shared between images and video)
    NSUrl referenceURL = e.Info[new NSString("UIImagePickerControllerReferenceURL")] as NSUrl;
    if (referenceURL != null)
        Console.WriteLine("Url:"+referenceURL.ToString ());

    // if it was an image, get the other image info
    if(isImage) {
        // get the original image
        UIImage originalImage = e.Info[UIImagePickerController.OriginalImage] as UIImage;
        if(originalImage != null) {
            // do something with the image
            Console.WriteLine ("got the original image");
            imageView.Image = originalImage; // display
        }
    } else { // if it's a video
        // get video url
        NSUrl mediaURL = e.Info[UIImagePickerController.MediaURL] as NSUrl;
        if(mediaURL != null) {
            Console.WriteLine(mediaURL.ToString());
        }
    }
    // dismiss the picker
    imagePicker.DismissModalViewControllerAnimated (true);
}
  1. Implement the cancellation handler method so that the picker will disappear:
void Handle_Canceled (object sender, EventArgs e) {
       imagePicker.DismissModalViewControllerAnimated(true);
}
  1. Open the Info.plist file and select the Source tab at the bottom. Click on a new line to add a new property. From the drop down list select Privacy – Photo Library Usage Description and enter a string value to describe why the user should allow access:

    Select Privacy Key

Note You must add the key/value pair into the Info.plist file. This allows the user to grant your app permission to use the Photo library. If you don't, your app will crash.

Additional Information

In the finished handler you can also access additional properties of the image (such as whether there is an edited version):

// get the edited image
UIImage editedImage = e.Info[UIImagePickerController.EditedImage] as UIImage;
if(editedImage != null) {
    // do something with the image
    Console.WriteLine ("got the edited image");
    imageView.Image = editedImage; // display
}

Xamarin Workbook

If it's not already installed, install the Xamarin Workbooks app first. The workbook file should download automatically, but if it doesn't, just click to start the workbook download manually.