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 is 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 want just 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 the 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 application 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 allow you to get user choose video file path. The method bellow 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 bellow:

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 good solution for this. But as workaround if you want to save pickture locally with yout app, and have an ability to go back to that picture with future app launches, you can always use the Local Storage API.

So fro example. You asked user to pick an image from gallary, and use that image as his profile cover for example. So here is how you can implment the following bahaviour.

//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 is 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 bellow:

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 bellow.

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 you need to first 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 bellow:

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

With means, 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 bellow:

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 will start camera interaction for the first time. If a user will approve permission, permission dialog will not appear with future camera usage. If a user will deny permission, camera access for your application will be blocked, and you will not able to present permissions dialog again.