blog

Home / DeveloperSection / Blogs / Selecting Image from the Gallery

Selecting Image from the Gallery

Tarun Kumar3326 30-Nov-2015

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 framework which provides helper classes like UIKit framework.

AVFoundation framework provides a class UIImagePickerController that provide rich support for working with images and videos. Here we will see how we can choose an image from the device gallery. 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:

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 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 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
}

Updated 13-Mar-2018

Leave Comment

Comments

Liked By