Skip to content

UIImage Picker Controller

Stanislav Osipov edited this page Jun 19, 2020 · 4 revisions

The ISN_UIImagePickerController manages the system interfaces for taking pictures, recording movies, and choosing items from the user's media library.

An image picker controller manages user interactions and delivers the results of those interactions. The role and appearance of an image picker controller depend on the ISN_UIImagePickerControllerSourceType you assign to it before you present it.

Before attemptingto use ISN_UIImagePickerController it's recommended you call IsSourceTypeAvailable method to ensure that the desired source type is available. It returns a boolean value indicating whether the device supports picking media using the specified source type. Because media source may not be present or may be unavailable, devices may not always support all source types. For example, if you attempt to pick an image from the user’s library and the library is empty this method returns false. Similarly, if the camera is already in use, this method returns false.

using SA.iOS.UIKit;
...
var source = ISN_UIImagePickerControllerSourceType.PhotoLibrary;
bool isAvailable = ISN_UIImagePickerController.IsSourceTypeAvailable(source);

You can also see what kind of media types are available on a current device for a particular source. For example, some iOS devices support video recording and some not. Use the GetAvailableMediaTypes method, along with the IsSourceTypeAvailable method, to determine if video recording is available on a device.

using SA.iOS.UIKit;
...
var source = ISN_UIImagePickerControllerSourceType.Camera;
List<string> availableMediaType = ISN_UIImagePickerController.GetAvailableMediaTypes(source);
foreach(var mediaType in availableMediaType) {
    Debug.Log(mediaType);
}

This isn't required, but could a very good practice to do it, before proceeding to the next articles, that explain how topic and save media with Camera, Album, and Photo Library sources.

Access Authorizations

Before you start working with the user photo library or camera, make sure the user authorized your application access.

Based on the source you use, you may want to check for Camera or Photos Permissions.

ModalPresentationStyle

Modal presentation styles available when presenting view controllers. All available options are listed in the ISN_UIModalPresentationStyle You can assign the view controller presentation style using the ISN_UIImagePickerController.ModalPresentationStyle property.

Please note that Automatic option is only available for iOS version 13 or higher. So you might want to check MajorOSVersion before presenting the view controller.

Use Samples

When you want to choose an item from use photo library, you need to present the configured UI picker controller.

First of all, let's create one:

var picker = new ISN_UIImagePickerController();

Prior to running the picker interface, set SourceType property value to the desired source type. The source type you set must be available and an exception is thrown if it is not. If you change this property while the picker is visible, the picker interface changes to match the new value in this property. The various source types are listed in the ISN_UIImagePickerControllerSourceType enumeration. The default value is ISN_UIImagePickerControllerSourceType.PhotoLibrary.

picker.SourceType = ISN_UIImagePickerControllerSourceType.Album;

Note: With ISN_UIImagePickerController you may set any picker source listed inside the ISN_UIImagePickerControllerSourceType enum.

Depending on the media types you assign to this property, the picker displays a dedicated interface for still images or movies or a selection control that lets the user choose the picker interface. Before setting this property, check which media types are available by calling the ISN_UIImagePickerController.GetAvailableMediaTypes method. If you set this property to an empty array, or to an array in which none of the media types is available for the current source, the system throws an exception.

For example, if we okay to allow a user to select an image or video:

picker.MediaTypes = new List<string>() { ISN_UIMediaType.IMAGE, ISN_UIMediaType.MOVIE };

If we choosing an image, we can always specify max image size, most likely you do not want to get the 10MP image back to Unity. And we also can set preferred compression format. My recommendation, use JPEG if you do not need image alpha and PNG if alpha is mattered to you. For the JPEG images, you can also specify compression rate. Allo those setting are optional. The code snippet below demonstrates default picker configuration.

picker.MaxImageSize = 512;
picker.ImageCompressionFormat = ISN_UIImageCompressionFormat.JPEG;
picker.ImageCompressionRate = 0.8f;

Once picker configuration is done, we only need to present the picker instance and analyze the result.

ISN_UIImagePickerController picker = new ISN_UIImagePickerController();
picker.SourceType = ISN_UIImagePickerControllerSourceType.Album;
picker.MediaTypes = new List<string>() { ISN_UIMediaType.IMAGE,  ISN_UIMediaType.MOVIE};
picker.MaxImageSize = 512;
picker.ImageCompressionFormat = ISN_UIImageCompressionFormat.JPEG;
picker.ImageCompressionRate = 0.8f;

picker.Present((result) => {
    if (result.IsSucceeded) {
        switch(result.MediaType) {
            case ISN_UIMediaType.IMAGE:
                Debug.Log("IMAGE local path: " + result.ImageURL);
                m_image.sprite = result.Image.ToSprite();
                m_go.GetComponent<Renderer>().material.mainTexture = result.Image;
                break;
            case ISN_UIMediaType.MOVIE:
                Debug.Log("MOVIE local path: " + result.MediaURL);
                Texture2D image = ISN_AVAssetImageGenerator.CopyCGImageAtTime(result.MediaURL, 0);
                m_image.sprite = image.ToSprite();
                m_go.GetComponent<Renderer>().material.mainTexture = image;
                break;
        }
    } else {
        Debug.Log("Madia picker failed with reason: " + result.Error.Message);
    }
});

Check out another smaller samples for ISN_UIImagePickerController

About

Foundation

AV Foundation

App Tracking Transparency

Game Kit

Store Kit

UI Kit

Social

Replay Kit

Contacts

AVKit

Photos

App Delegate

User Notifications

MediaPlayer

Core Location

AdSupport

EventKit

CloudKit

Authentication Services

XCode

Knowledge Base

Clone this wiki locally