Add support for selecting media from library
Summary:
This PR adds support for UIImagePickerController to allow selecting a photo / video from the users camera roll.
![ios simulator screen shot jun 14 2015 4 50 03 pm](https://cloud.githubusercontent.com/assets/688326/8147758/ae6dc8d4-12b6-11e5-80f0-2bcaa964a5d8.png)
Example:
Selecting something from camera roll
```
ImagePickerIOS.openSelectDialog(<config>, <successCallback>, <cancelCallback>);
ImagePickerIOS.openSelectDialog({
showImages: true, // defaults to true
showVideos: false // defaults to false
}, function (data) {
console.info("Got a callback!");
console.info(data); // file URL as in assets-library://asset/asset.JPG?id=E2741A73-D185-44B6-A2E6-2D55F69CD088&ext=JPG
}, function() {
console.info("Cancelled");
});
```
Using camera
```
ImagePickerIOS.openCameraDialog(<config>, <successCallback>, <cancelCallback>);
ImagePickerIOS.openSelectDialog({
videoMode: false, // defaults to true, whether to record videos instead
}, function (data) {
console.info("Got
Closes https://github.com/facebook/react-native/pull/1620
Github Author: David Mohl <me@dave.cx>
Test Plan: Imported from GitHub, without a `Test Plan:` line.
2015-06-22 10:06:47 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2013, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#import "RCTImagePickerManager.h"
|
|
|
|
#import "RCTRootView.h"
|
2015-09-22 17:43:56 +00:00
|
|
|
#import "RCTLog.h"
|
|
|
|
#import "RCTUtils.h"
|
Add support for selecting media from library
Summary:
This PR adds support for UIImagePickerController to allow selecting a photo / video from the users camera roll.
![ios simulator screen shot jun 14 2015 4 50 03 pm](https://cloud.githubusercontent.com/assets/688326/8147758/ae6dc8d4-12b6-11e5-80f0-2bcaa964a5d8.png)
Example:
Selecting something from camera roll
```
ImagePickerIOS.openSelectDialog(<config>, <successCallback>, <cancelCallback>);
ImagePickerIOS.openSelectDialog({
showImages: true, // defaults to true
showVideos: false // defaults to false
}, function (data) {
console.info("Got a callback!");
console.info(data); // file URL as in assets-library://asset/asset.JPG?id=E2741A73-D185-44B6-A2E6-2D55F69CD088&ext=JPG
}, function() {
console.info("Cancelled");
});
```
Using camera
```
ImagePickerIOS.openCameraDialog(<config>, <successCallback>, <cancelCallback>);
ImagePickerIOS.openSelectDialog({
videoMode: false, // defaults to true, whether to record videos instead
}, function (data) {
console.info("Got
Closes https://github.com/facebook/react-native/pull/1620
Github Author: David Mohl <me@dave.cx>
Test Plan: Imported from GitHub, without a `Test Plan:` line.
2015-06-22 10:06:47 +00:00
|
|
|
|
|
|
|
#import <UIKit/UIKit.h>
|
|
|
|
|
|
|
|
#import <MobileCoreServices/UTCoreTypes.h>
|
|
|
|
|
|
|
|
@interface RCTImagePickerManager ()<UIImagePickerControllerDelegate, UINavigationControllerDelegate>
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation RCTImagePickerManager
|
|
|
|
{
|
|
|
|
NSMutableArray *_pickers;
|
|
|
|
NSMutableArray *_pickerCallbacks;
|
|
|
|
NSMutableArray *_pickerCancelCallbacks;
|
|
|
|
}
|
|
|
|
|
|
|
|
RCT_EXPORT_MODULE(ImagePickerIOS);
|
|
|
|
|
|
|
|
- (instancetype)init
|
|
|
|
{
|
|
|
|
if ((self = [super init])) {
|
2015-08-17 14:35:34 +00:00
|
|
|
_pickers = [NSMutableArray new];
|
|
|
|
_pickerCallbacks = [NSMutableArray new];
|
|
|
|
_pickerCancelCallbacks = [NSMutableArray new];
|
Add support for selecting media from library
Summary:
This PR adds support for UIImagePickerController to allow selecting a photo / video from the users camera roll.
![ios simulator screen shot jun 14 2015 4 50 03 pm](https://cloud.githubusercontent.com/assets/688326/8147758/ae6dc8d4-12b6-11e5-80f0-2bcaa964a5d8.png)
Example:
Selecting something from camera roll
```
ImagePickerIOS.openSelectDialog(<config>, <successCallback>, <cancelCallback>);
ImagePickerIOS.openSelectDialog({
showImages: true, // defaults to true
showVideos: false // defaults to false
}, function (data) {
console.info("Got a callback!");
console.info(data); // file URL as in assets-library://asset/asset.JPG?id=E2741A73-D185-44B6-A2E6-2D55F69CD088&ext=JPG
}, function() {
console.info("Cancelled");
});
```
Using camera
```
ImagePickerIOS.openCameraDialog(<config>, <successCallback>, <cancelCallback>);
ImagePickerIOS.openSelectDialog({
videoMode: false, // defaults to true, whether to record videos instead
}, function (data) {
console.info("Got
Closes https://github.com/facebook/react-native/pull/1620
Github Author: David Mohl <me@dave.cx>
Test Plan: Imported from GitHub, without a `Test Plan:` line.
2015-06-22 10:06:47 +00:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
RCT_EXPORT_METHOD(canRecordVideos:(RCTResponseSenderBlock)callback)
|
|
|
|
{
|
|
|
|
NSArray *availableMediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
|
|
|
|
callback(@[@([availableMediaTypes containsObject:(NSString *)kUTTypeMovie])]);
|
|
|
|
}
|
|
|
|
|
|
|
|
RCT_EXPORT_METHOD(canUseCamera:(RCTResponseSenderBlock)callback)
|
|
|
|
{
|
|
|
|
callback(@[@([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])]);
|
|
|
|
}
|
|
|
|
|
|
|
|
RCT_EXPORT_METHOD(openCameraDialog:(NSDictionary *)config
|
|
|
|
successCallback:(RCTResponseSenderBlock)callback
|
|
|
|
cancelCallback:(RCTResponseSenderBlock)cancelCallback)
|
|
|
|
{
|
2015-09-22 17:43:56 +00:00
|
|
|
if (RCTRunningInAppExtension()) {
|
|
|
|
cancelCallback(@[@"Camera access is unavailable in an app extension"]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
UIWindow *keyWindow = RCTSharedApplication().keyWindow;
|
Add support for selecting media from library
Summary:
This PR adds support for UIImagePickerController to allow selecting a photo / video from the users camera roll.
![ios simulator screen shot jun 14 2015 4 50 03 pm](https://cloud.githubusercontent.com/assets/688326/8147758/ae6dc8d4-12b6-11e5-80f0-2bcaa964a5d8.png)
Example:
Selecting something from camera roll
```
ImagePickerIOS.openSelectDialog(<config>, <successCallback>, <cancelCallback>);
ImagePickerIOS.openSelectDialog({
showImages: true, // defaults to true
showVideos: false // defaults to false
}, function (data) {
console.info("Got a callback!");
console.info(data); // file URL as in assets-library://asset/asset.JPG?id=E2741A73-D185-44B6-A2E6-2D55F69CD088&ext=JPG
}, function() {
console.info("Cancelled");
});
```
Using camera
```
ImagePickerIOS.openCameraDialog(<config>, <successCallback>, <cancelCallback>);
ImagePickerIOS.openSelectDialog({
videoMode: false, // defaults to true, whether to record videos instead
}, function (data) {
console.info("Got
Closes https://github.com/facebook/react-native/pull/1620
Github Author: David Mohl <me@dave.cx>
Test Plan: Imported from GitHub, without a `Test Plan:` line.
2015-06-22 10:06:47 +00:00
|
|
|
UIViewController *rootViewController = keyWindow.rootViewController;
|
|
|
|
|
2015-08-17 14:35:34 +00:00
|
|
|
UIImagePickerController *imagePicker = [UIImagePickerController new];
|
Add support for selecting media from library
Summary:
This PR adds support for UIImagePickerController to allow selecting a photo / video from the users camera roll.
![ios simulator screen shot jun 14 2015 4 50 03 pm](https://cloud.githubusercontent.com/assets/688326/8147758/ae6dc8d4-12b6-11e5-80f0-2bcaa964a5d8.png)
Example:
Selecting something from camera roll
```
ImagePickerIOS.openSelectDialog(<config>, <successCallback>, <cancelCallback>);
ImagePickerIOS.openSelectDialog({
showImages: true, // defaults to true
showVideos: false // defaults to false
}, function (data) {
console.info("Got a callback!");
console.info(data); // file URL as in assets-library://asset/asset.JPG?id=E2741A73-D185-44B6-A2E6-2D55F69CD088&ext=JPG
}, function() {
console.info("Cancelled");
});
```
Using camera
```
ImagePickerIOS.openCameraDialog(<config>, <successCallback>, <cancelCallback>);
ImagePickerIOS.openSelectDialog({
videoMode: false, // defaults to true, whether to record videos instead
}, function (data) {
console.info("Got
Closes https://github.com/facebook/react-native/pull/1620
Github Author: David Mohl <me@dave.cx>
Test Plan: Imported from GitHub, without a `Test Plan:` line.
2015-06-22 10:06:47 +00:00
|
|
|
imagePicker.delegate = self;
|
|
|
|
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
|
|
|
|
|
|
|
|
if ([config[@"videoMode"] boolValue]) {
|
|
|
|
imagePicker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModeVideo;
|
|
|
|
}
|
|
|
|
|
|
|
|
[_pickers addObject:imagePicker];
|
|
|
|
[_pickerCallbacks addObject:callback];
|
|
|
|
[_pickerCancelCallbacks addObject:cancelCallback];
|
|
|
|
|
|
|
|
[rootViewController presentViewController:imagePicker animated:YES completion:nil];
|
|
|
|
}
|
|
|
|
|
|
|
|
RCT_EXPORT_METHOD(openSelectDialog:(NSDictionary *)config
|
|
|
|
successCallback:(RCTResponseSenderBlock)callback
|
|
|
|
cancelCallback:(RCTResponseSenderBlock)cancelCallback)
|
|
|
|
{
|
2015-09-22 17:43:56 +00:00
|
|
|
if (RCTRunningInAppExtension()) {
|
|
|
|
cancelCallback(@[@"Image picker is currently unavailable in an app extension"]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
UIWindow *keyWindow = RCTSharedApplication().keyWindow;
|
Add support for selecting media from library
Summary:
This PR adds support for UIImagePickerController to allow selecting a photo / video from the users camera roll.
![ios simulator screen shot jun 14 2015 4 50 03 pm](https://cloud.githubusercontent.com/assets/688326/8147758/ae6dc8d4-12b6-11e5-80f0-2bcaa964a5d8.png)
Example:
Selecting something from camera roll
```
ImagePickerIOS.openSelectDialog(<config>, <successCallback>, <cancelCallback>);
ImagePickerIOS.openSelectDialog({
showImages: true, // defaults to true
showVideos: false // defaults to false
}, function (data) {
console.info("Got a callback!");
console.info(data); // file URL as in assets-library://asset/asset.JPG?id=E2741A73-D185-44B6-A2E6-2D55F69CD088&ext=JPG
}, function() {
console.info("Cancelled");
});
```
Using camera
```
ImagePickerIOS.openCameraDialog(<config>, <successCallback>, <cancelCallback>);
ImagePickerIOS.openSelectDialog({
videoMode: false, // defaults to true, whether to record videos instead
}, function (data) {
console.info("Got
Closes https://github.com/facebook/react-native/pull/1620
Github Author: David Mohl <me@dave.cx>
Test Plan: Imported from GitHub, without a `Test Plan:` line.
2015-06-22 10:06:47 +00:00
|
|
|
UIViewController *rootViewController = keyWindow.rootViewController;
|
|
|
|
|
2015-08-17 14:35:34 +00:00
|
|
|
UIImagePickerController *imagePicker = [UIImagePickerController new];
|
Add support for selecting media from library
Summary:
This PR adds support for UIImagePickerController to allow selecting a photo / video from the users camera roll.
![ios simulator screen shot jun 14 2015 4 50 03 pm](https://cloud.githubusercontent.com/assets/688326/8147758/ae6dc8d4-12b6-11e5-80f0-2bcaa964a5d8.png)
Example:
Selecting something from camera roll
```
ImagePickerIOS.openSelectDialog(<config>, <successCallback>, <cancelCallback>);
ImagePickerIOS.openSelectDialog({
showImages: true, // defaults to true
showVideos: false // defaults to false
}, function (data) {
console.info("Got a callback!");
console.info(data); // file URL as in assets-library://asset/asset.JPG?id=E2741A73-D185-44B6-A2E6-2D55F69CD088&ext=JPG
}, function() {
console.info("Cancelled");
});
```
Using camera
```
ImagePickerIOS.openCameraDialog(<config>, <successCallback>, <cancelCallback>);
ImagePickerIOS.openSelectDialog({
videoMode: false, // defaults to true, whether to record videos instead
}, function (data) {
console.info("Got
Closes https://github.com/facebook/react-native/pull/1620
Github Author: David Mohl <me@dave.cx>
Test Plan: Imported from GitHub, without a `Test Plan:` line.
2015-06-22 10:06:47 +00:00
|
|
|
imagePicker.delegate = self;
|
|
|
|
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
|
|
|
|
|
2015-08-17 14:35:34 +00:00
|
|
|
NSMutableArray *allowedTypes = [NSMutableArray new];
|
Add support for selecting media from library
Summary:
This PR adds support for UIImagePickerController to allow selecting a photo / video from the users camera roll.
![ios simulator screen shot jun 14 2015 4 50 03 pm](https://cloud.githubusercontent.com/assets/688326/8147758/ae6dc8d4-12b6-11e5-80f0-2bcaa964a5d8.png)
Example:
Selecting something from camera roll
```
ImagePickerIOS.openSelectDialog(<config>, <successCallback>, <cancelCallback>);
ImagePickerIOS.openSelectDialog({
showImages: true, // defaults to true
showVideos: false // defaults to false
}, function (data) {
console.info("Got a callback!");
console.info(data); // file URL as in assets-library://asset/asset.JPG?id=E2741A73-D185-44B6-A2E6-2D55F69CD088&ext=JPG
}, function() {
console.info("Cancelled");
});
```
Using camera
```
ImagePickerIOS.openCameraDialog(<config>, <successCallback>, <cancelCallback>);
ImagePickerIOS.openSelectDialog({
videoMode: false, // defaults to true, whether to record videos instead
}, function (data) {
console.info("Got
Closes https://github.com/facebook/react-native/pull/1620
Github Author: David Mohl <me@dave.cx>
Test Plan: Imported from GitHub, without a `Test Plan:` line.
2015-06-22 10:06:47 +00:00
|
|
|
if ([config[@"showImages"] boolValue]) {
|
|
|
|
[allowedTypes addObject:(NSString *)kUTTypeImage];
|
|
|
|
}
|
|
|
|
if ([config[@"showVideos"] boolValue]) {
|
|
|
|
[allowedTypes addObject:(NSString *)kUTTypeMovie];
|
|
|
|
}
|
|
|
|
|
|
|
|
imagePicker.mediaTypes = allowedTypes;
|
|
|
|
|
|
|
|
[_pickers addObject:imagePicker];
|
|
|
|
[_pickerCallbacks addObject:callback];
|
|
|
|
[_pickerCancelCallbacks addObject:cancelCallback];
|
|
|
|
|
|
|
|
[rootViewController presentViewController:imagePicker animated:YES completion:nil];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)imagePickerController:(UIImagePickerController *)picker
|
|
|
|
didFinishPickingMediaWithInfo:(NSDictionary *)info
|
|
|
|
{
|
|
|
|
NSUInteger index = [_pickers indexOfObject:picker];
|
|
|
|
RCTResponseSenderBlock callback = _pickerCallbacks[index];
|
|
|
|
|
|
|
|
[_pickers removeObjectAtIndex:index];
|
|
|
|
[_pickerCallbacks removeObjectAtIndex:index];
|
|
|
|
[_pickerCancelCallbacks removeObjectAtIndex:index];
|
|
|
|
|
2015-09-22 17:43:56 +00:00
|
|
|
UIWindow *keyWindow = RCTSharedApplication().keyWindow;
|
Add support for selecting media from library
Summary:
This PR adds support for UIImagePickerController to allow selecting a photo / video from the users camera roll.
![ios simulator screen shot jun 14 2015 4 50 03 pm](https://cloud.githubusercontent.com/assets/688326/8147758/ae6dc8d4-12b6-11e5-80f0-2bcaa964a5d8.png)
Example:
Selecting something from camera roll
```
ImagePickerIOS.openSelectDialog(<config>, <successCallback>, <cancelCallback>);
ImagePickerIOS.openSelectDialog({
showImages: true, // defaults to true
showVideos: false // defaults to false
}, function (data) {
console.info("Got a callback!");
console.info(data); // file URL as in assets-library://asset/asset.JPG?id=E2741A73-D185-44B6-A2E6-2D55F69CD088&ext=JPG
}, function() {
console.info("Cancelled");
});
```
Using camera
```
ImagePickerIOS.openCameraDialog(<config>, <successCallback>, <cancelCallback>);
ImagePickerIOS.openSelectDialog({
videoMode: false, // defaults to true, whether to record videos instead
}, function (data) {
console.info("Got
Closes https://github.com/facebook/react-native/pull/1620
Github Author: David Mohl <me@dave.cx>
Test Plan: Imported from GitHub, without a `Test Plan:` line.
2015-06-22 10:06:47 +00:00
|
|
|
UIViewController *rootViewController = keyWindow.rootViewController;
|
|
|
|
[rootViewController dismissViewControllerAnimated:YES completion:nil];
|
|
|
|
|
|
|
|
callback(@[[info[UIImagePickerControllerReferenceURL] absoluteString]]);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
|
|
|
|
{
|
|
|
|
NSUInteger index = [_pickers indexOfObject:picker];
|
|
|
|
RCTResponseSenderBlock callback = _pickerCancelCallbacks[index];
|
|
|
|
|
|
|
|
[_pickers removeObjectAtIndex:index];
|
|
|
|
[_pickerCallbacks removeObjectAtIndex:index];
|
|
|
|
[_pickerCancelCallbacks removeObjectAtIndex:index];
|
|
|
|
|
2015-09-22 17:43:56 +00:00
|
|
|
UIWindow *keyWindow = RCTSharedApplication().keyWindow;
|
Add support for selecting media from library
Summary:
This PR adds support for UIImagePickerController to allow selecting a photo / video from the users camera roll.
![ios simulator screen shot jun 14 2015 4 50 03 pm](https://cloud.githubusercontent.com/assets/688326/8147758/ae6dc8d4-12b6-11e5-80f0-2bcaa964a5d8.png)
Example:
Selecting something from camera roll
```
ImagePickerIOS.openSelectDialog(<config>, <successCallback>, <cancelCallback>);
ImagePickerIOS.openSelectDialog({
showImages: true, // defaults to true
showVideos: false // defaults to false
}, function (data) {
console.info("Got a callback!");
console.info(data); // file URL as in assets-library://asset/asset.JPG?id=E2741A73-D185-44B6-A2E6-2D55F69CD088&ext=JPG
}, function() {
console.info("Cancelled");
});
```
Using camera
```
ImagePickerIOS.openCameraDialog(<config>, <successCallback>, <cancelCallback>);
ImagePickerIOS.openSelectDialog({
videoMode: false, // defaults to true, whether to record videos instead
}, function (data) {
console.info("Got
Closes https://github.com/facebook/react-native/pull/1620
Github Author: David Mohl <me@dave.cx>
Test Plan: Imported from GitHub, without a `Test Plan:` line.
2015-06-22 10:06:47 +00:00
|
|
|
UIViewController *rootViewController = keyWindow.rootViewController;
|
|
|
|
[rootViewController dismissViewControllerAnimated:YES completion:nil];
|
|
|
|
|
|
|
|
callback(@[]);
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|