使用 React Native 组件的 UIImagePickerController 叠加视图

UIImagePickerController overlay view using React Native components

我想使用 React Native 组件定义一个 UIImagePickerController 覆盖视图。我正在设置为 Marc Shilling fabulous react-native-image-picker 添加一个新选项。我希望 ImagePickerManager.launchCamera 函数将 <View> 和子项作为其参数之一,渲染它,并将其传递给 UIImagePickerController.cameraOverlayView。我做到了这一点:

在ImagePickerManager.m中:

NSNumber *viewTag = [self.options valueForKey:@"overlayNode"];
if (![viewTag isEqual:[NSNull null]] && viewTag != 0) {
    UIView *view = [_bridge.uiManager viewForReactTag:viewTag];
    [self.picker setCameraOverlayView:view];
}

但这需要我在 JS 端做一些扭曲才能获得组件实例。

React.createClass({
    render: function() { 
        return (
            <View>
                <TouchableHighlight onPress={this._showCamera}><Text>Camera</Text></TouchableHighlight>
                <View ref="foo"><Text>On top of the camera!</Text></View>
            </View>
        );
    },
    _showCamera: function() {
        var options = {
            overlayNode: React.findNodeHandle(this.refs.foo),
        };
        ImagePickerManager.launchCamera(options, (response)  => {

        });
    }
});

如果我尝试说 overlayNode: React.findNodeHandle(<View ref="foo"><Text>On top of the camera!</Text></View>),我会得到一个红框。

我尝试了一些创建新 RCTRootView 并通过 AppRegistry.registerComponent 注册叠加层的操作,但我没有看到屏幕上实际呈现任何内容。

NSString *overlayRootName = [self.options valueForKey:@"overlayComponentName"];
if (![overlayRootName isEqual:[NSNull null]] && overlayRootName.length != 0) {
    RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:self.bridge moduleName:overlayRootName initialProperties:nil];
    [self.picker setCameraOverlayView:rootView];
}

我在这里错过了什么?

后一种解决方案(声明替代方案 RCTRootView)最终成为对我有用的解决方案。我遇到的问题是,由于某种原因,我通过添加 @synthesize bridge = _bridge;ImagePickerManager 中获得的桥在 initWithBridge.

中使用时没有呈现任何内容

相反,我在 AppDelegate 中添加了一个字段,公开了在 application: didFinishLaunchingWithOptions: 中创建的桥,就像 in this hybrid app example 所做的那样。当我在 initWithBridge 中使用 that 桥时,叠加层会呈现在屏幕上。还要确保将 rootView.framerootView.backgroundColor 明确设置为透明。