---
title: "Selecting Image from the Gallery"  
description: "In iOS application development if we want to fetch media files like Images or Videos then we will use media APIs like AVFoundation. AVFoundation is a"  
author: "Tarun Kumar"  
published: 2015-11-30  
updated: 2018-03-13  
canonical: https://www.mindstick.com/blog/10993/selecting-image-from-the-gallery  
category: "iphone"  
tags: ["iphone", "ios", "objective c"]  
reading_time: 3 minutes  

---

# Selecting Image from the Gallery

In **iOS [application development](https://www.mindstick.com/articles/65303/8-benefits-of-android-application-development-for-online-business)** if we want to fetch [media files](https://answers.mindstick.com/qa/50652/how-to-back-up-apps-contacts-photos-and-media-files-on-the-iphone-8-plus) like Images or Videos then we will use media APIs like AVFoundation. AVFoundation is a [framework](https://www.mindstick.com/forum/34615/software-framework-vs-library) which provides helper classes like UIKit framework.\

AVFoundation framework provides a class UIImagePickerController that provide rich support for working with [images and videos](https://www.mindstick.com/forum/158315/how-do-you-create-responsive-images-and-videos-using-bootstrap-classes). Here we will see how we can choose an image from the device gallery. [Applications](https://www.mindstick.com/articles/12847/how-to-choose-the-right-ethernet-cable-for-industrial-applications) can ask the user to select an image or video from our Photo Albums using UIImagePickerController.

To implement the UIImagePickerController use following codes:

- For creating image picker control:

```
imagePicker=new UIImagePickerController(); 
```

- For setting source/media type:

```
imagePicker.SourceType = UIImagePickerControllerSourceType.PhotoLibrary;
imagePicker.MediaTypes = UIImagePickerController.AvailableMediaTypes (UIImagePickerControllerSourceType.PhotoLibrary);
```

- For assigning delegate handler:

```
imagePicker.FinishedPickingMedia += Handle_FinishedPickingMedia;
imagePicker.Canceled += Handle_Canceled;
```

- For displaying the [controller](https://www.mindstick.com/blog/273/passing-values-from-controller-to-view-in-asp-dot-net-mvc):

```
NavigationController.PresentModalViewController(imagePicker, true);
```

- For implementing the handler method which will checks which kind of selection user selected and if it is an image then image will be displayed:

```
protected void Handle_FinishedPickingMedia (object sender, UIImagePickerMediaPickedEventArgs e)
{
    // this code is used for determining which is selected means 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;
    }
    // For getting common information shared between images and video
    NSUrl referenceURL = e.Info[new NSString("UIImagePickerControllerReferenceUrl")] as NSUrl;
    if (referenceURL != null)
        Console.WriteLine("Url:" + referenceURL.ToString());
    // if it is an image then get the image informatino
    if(isImage)
    {
        // for getting the original image use this code
        UIImage originalImage = e.Info[UIImagePickerController.OriginalImage] as UIImage;
        if(originalImage != null) {
        // here we can do something with the image
        Console.WriteLine (“got the original image”);
        imageView.Image = originalImage;
    }
} else {
    NSUrl mediaURL = e.Info[UIImagePickerController.MediaURL] as NSUrl;
    if(mediaURL != null)
    {
        Console.WriteLine(mediaURL.ToString());
    }
}
// use this for dismiss the picker controller
imagePicker.DismissModalViewControllerAnimated (true);
}
```

- For cancelling the handler method implement the [cancellation](https://answers.mindstick.com/qa/46736/should-you-buy-cancellation-travel-insurance) handler method so that the picker will be disappear:

```
void Handle_Canceled (object sender, EventArgs e)
{
       imagePicker.DismissModalViewControllerAnimated(true);
}
```

- We can also access the additional [properties](https://www.mindstick.com/articles/23331/nootropics-7-different-types-and-their-unique-properties) of the image if edited version is available:

```
// for getting the edited image use this code
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
}
```

---

Original Source: https://www.mindstick.com/blog/10993/selecting-image-from-the-gallery

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
