Files
react-native/Libraries/Image/ImagePickerIOS.js
T
Albert Sun 63769518e0 Fix selecting videos from library in iOS 13
Summary:
In iOS 13, Apple made a change that results in video URLs returned by UIImagePickerController becoming invalidated as soon as the info object from the delegate callback is released. This commit works around this issue by retaining these info objects by default and giving the application a way to release them once it is done processing the video.

See also https://stackoverflow.com/questions/57798968/didfinishpickingmediawithinfo-returns-different-url-in-ios-13

Reviewed By: olegbl, mmmulani

Differential Revision: D17845889

fbshipit-source-id: 12d0e496508dafa2581ef12730f7537ef98c60e2
2019-10-11 16:33:19 -07:00

106 lines
3.1 KiB
JavaScript

/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @flow
*/
'use strict';
import NativeImagePickerIOS from './NativeImagePickerIOS';
import invariant from 'invariant';
const ImagePickerIOS = {
canRecordVideos: function(callback: (result: boolean) => void): void {
invariant(NativeImagePickerIOS, 'ImagePickerIOS is not available');
return NativeImagePickerIOS.canRecordVideos(callback);
},
canUseCamera: function(callback: (result: boolean) => void): void {
invariant(NativeImagePickerIOS, 'ImagePickerIOS is not available');
return NativeImagePickerIOS.canUseCamera(callback);
},
openCameraDialog: function(
config: $ReadOnly<{|
unmirrorFrontFacingCamera?: boolean,
videoMode?: boolean,
|}>,
successCallback: (imageURL: string, height: number, width: number) => void,
cancelCallback: () => void,
): void {
invariant(NativeImagePickerIOS, 'ImagePickerIOS is not available');
var newConfig = {
videoMode: true,
unmirrorFrontFacingCamera: false,
};
if (config.videoMode != null) {
newConfig.videoMode = config.videoMode;
}
if (config.unmirrorFrontFacingCamera != null) {
newConfig.unmirrorFrontFacingCamera = config.unmirrorFrontFacingCamera;
}
return NativeImagePickerIOS.openCameraDialog(
newConfig,
successCallback,
cancelCallback,
);
},
openSelectDialog: function(
config: $ReadOnly<{|
showImages?: boolean,
showVideos?: boolean,
|}>,
successCallback: (imageURL: string, height: number, width: number) => void,
cancelCallback: () => void,
): void {
invariant(NativeImagePickerIOS, 'ImagePickerIOS is not available');
var newConfig = {
showImages: true,
showVideos: false,
};
if (config.showImages != null) {
newConfig.showImages = config.showImages;
}
if (config.showVideos != null) {
newConfig.showVideos = config.showVideos;
}
return NativeImagePickerIOS.openSelectDialog(
newConfig,
successCallback,
cancelCallback,
);
},
/**
* In iOS 13, the video URLs returned by the Image Picker are invalidated when
* the picker is dismissed, unless reference to it is held. This API allows
* the application to signal when it's finished with the video so that the
* reference can be cleaned up.
* It is safe to call this method for urlsthat aren't video URLs;
* it will be a no-op.
*/
removePendingVideo: function(url: string): void {
invariant(NativeImagePickerIOS, 'ImagePickerIOS is not available');
NativeImagePickerIOS.removePendingVideo(url);
},
/**
* WARNING: In most cases, removePendingVideo should be used instead because
* clearAllPendingVideos could clear out pending videos made by other callers.
*/
clearAllPendingVideos: function(): void {
invariant(NativeImagePickerIOS, 'ImagePickerIOS is not available');
NativeImagePickerIOS.clearAllPendingVideos();
},
};
module.exports = ImagePickerIOS;