Camera And Gallery

Saving Texture2D to the camera roll.

Note: Texture Should be marked as readable.

IOSCamera.Instance.SaveTextureToCameraRoll(MyTexture2D)

or you can save the app screenshot using

IOSCamera.Instance.SaveScreenshotToCameraRoll();

You may use OnImageSaved action to find out if the image was successfully saved

IOSCamera.OnImageSaved += OnImageSaved;
private void OnImageSaved (SA.Common.Models.Result result) {
	IOSCamera.OnImageSaved -= OnImageSaved;
	if(result.IsSucceeded) {
		IOSMessage.Create("Success", "Image Successfully saved to Camera Roll");
	} else {
		IOSMessage.Create("ERROR", "Image Save Failed");
	}
}

Saved texture will appear in user photo album. But if you just want to save an image to an app local storage with ability to use this image later, you should look at Local Storage API.

Getting Texture2D from Camera or Gallery.

Subscribe to the image load event:

IOSCamera.OnImagePicked += OnImage;

Then retrieve the image with one of the following options:

IOSCamera.Instance.PickImage(ISN_ImageSource.Album);
IOSCamera.Instance.PickImage(ISN_ImageSource.Library);
IOSCamera.Instance.PickImage(ISN_ImageSource.Camera);

Example data retrieving after OnImagePicked action fired:


IOSCamera.OnImagePicked += OnImage;
private void OnImage (IOSImagePickResult result) {
        if(result.IsSucceeded) {
            darawTexgture = result.Image;
        }
        IOSCamera.OnImagePicked -= OnImage;
}

 

Note: You can choose between PNG and JPEG compression. PNG will allow you to load textures with Alpha, but JPEG will consume less memory.

Note: Managing multiple full-sized images can be tricky with limited memory. If you find your application running out of memory after displaying just a few images, you can dramatically reduce an amount of dynamic heap used by expanding the JPEG into a memory by setting low max image size or image JPEG compression type:

Window → IOS Native → Edit Settings

Note: On iPad the image picker may appear in portrait mode even if an application is  in landscape. As per the UIImagePickerController Class Reference.  Important: The UIImagePickerController class supports portrait mode only. This class is intended to be used as-is and does not support subclassing.

Getting Video from the Gallery.

The plugin allows you to get user choose video file path. The method below will bring up the native video pick dialog:

IOSCamera.Instance.GetVideoPathFromAlbum();

In order to get video path, you may subscribe to the OnVideoPathPicked event as showed on the code snippet below:

IOSCamera.OnVideoPathPicked += HandleOnVideoPathPicked;
IOSCamera.Instance.GetVideoPathFromAlbum();

void HandleOnVideoPathPicked (string path) {
	Debug.Log("Video Path: " + path)
}

Image Path

Lot's of users keep asking us how to get picked / saved image path. So far we haven't found a good solution for this. But as workaround if you want to save picture locally with your app and have an ability to go back to that picture while a future app launches, you can always use the Local Storage API.

So, for example: you asked a user to pick an image from gallary and use that image as his profile cover. So, here is how you can implement the following bahavior.

//Pick an image

IOSCamera.Instance.PickImage(ISN_ImageSource.Album);
IOSCamera.OnImagePicked += OnImage;

private void OnImage (IOSImagePickResult result) {
	if(result.IsSucceeded) {
        //Same if image was picked successfully
		SA.IOSNative.Storage.AppCache.Save ("COVER_IMAGE", result.Image);
	}

	IOSCamera.OnImagePicked -= OnImage;
}

//Now you can always load this image with:
Texture2D cover = SA.IOSNative.Storage.AppCache.GetTexture ("COVER_IMAGE");

Pick Multiple Images From Camera Roll

This feature is not available with standard IOS API. So we used custom 3-rd party solution for it, which means that if you need to pick more than one image at a time, you need to do extra instal actions. First of all, enable multi-picker API as showed in the screenshot below:

Note: Camera And Gallery API should be enabled as well.

Now you need to Add ELCImagePickerController to your project.

1) Download  ELCImagePicker from the GitHub.

2) Place files under your Plugins/IOS folder

Now you can get multiple textures from Camera Roll as shown on the code snippet below.

ISN_FilePicker.MediaPickFinished += async delegate(ISN_FilePickerResult res) {
	Debug.Log("Picked " + res.PickedImages.Count + " images");
};
ISN_FilePicker.Instance.PickFromCameraRoll();

Privacy Settings in iOS 10

It has long been the case with iOS that if you want to access a user’s private data, firstly, you need to ask the user for permission. In iOS 10 Apple is extending the scope of these privacy controls by including access to the user’s photo library, camera, media player, etc. List of affected frameworks can be found below:

Contacts, Calendar, Reminders, Photos, Bluetooth Sharing, Microphone, Camera, Location, Health, HomeKit, Media Library, Motion, CallKit, Speech Recognition, SiriKit, TV Provider.

It means that if you want to use Camera or Gallary API, you need to provide purpose string to a user. You can specify those string using IOS Native Settings menu as showed below:

Or you can also do this using the following variables:

IOSNativeSettings.Instance.CameraUsageDescription
IOSNativeSettings.Instance.PhotoLibraryUsageDescription

The system shows the purpose string when asking the user to allow access (so you may want to localize it):

The direction from Apple is clear. If you access private data, declare your intentions up front or expect your App to crash.

Request Permissions

Permission will be asked automatically when your app starts camera interaction for the first time. If a user approves a permission, the permission dialog will not appear with the future camera usage. If a user denies a permission, the camera access for your application will be blocked, and you will not be able to present permissions dialog again.