使用反应导航从相机回调图像uri

时间:2019-02-13 13:46:49

标签: react-native parameter-passing react-navigation

我正在使用“反应导航”创建两个选项卡,“主页”和“相机”,并且“相机”选项卡中有一个标题按钮。另外,它使用“ withNavigationFocus”来调用相机组件。

App.js文件:

const Home = createStackNavigator(
  {
    HomeStack: { screen: HomeScreen }
  },
  {
    defaultNavigationOptions: {
      headerStyle: {
        backgroundColor: '#42f44b',
      },
      headerTintColor: '#FFFFFF',
      title: 'Home',
    },
  }
);

const CameraStack = createStackNavigator(
   {
    // take pictures of the bill
    Camera: { screen: CameraControler}
  },
  {
    defaultNavigationOptions: ({navigation}) => ({
      headerStyle: {
        backgroundColor: '#42f44b',
      },
      headerTintColor: '#FFFFFF',
      title: 'Add a Bill',
      headerRight: (
        <View style={{margin: 10}}>
          <Button
             onPress={() => console.log("IMAGE URI IS NEEDED HERE")}
            title="Done"
            color="#1d8581"
              />
        </View>
      ),
    }),
  }
);

const TabHolder = createBottomTabNavigator(
  {
    //Defination of Navigaton bottom options
    Add: { screen: CameraStack },
    Home: { screen: HomeStack }
  },
  {
    defaultNavigationOptions: ({ navigation }) => ({
      tabBarIcon: ({ focused, horizontal, tintColor }) => {
        const { routeName } = navigation.state;
        let IconComponent = SimpleLineIcons;
        let iconName;
        if (routeName === 'Add') {
          iconName = `camera`;
        } else if (routeName === 'Home') {
          iconName = `home`;
        }

        return <IconComponent name={iconName} size={25} color={tintColor} />;
      },
    }),
    tabBarOptions: {
      activeTintColor: '#42f44b',
      inactiveTintColor: 'gray',
      showLabel: false
    },
    initialRouteName: "Home"
  }
);

const styles = StyleSheet.create({
  processButton: {
    marginRight: 10
  }
});

export default createAppContainer(TabHolder);

CameraController.js文件:

  import CameraScreen from './camerascreen';
  import { withNavigationFocus } from "react-navigation";

  class CameraControler extends React.Component {

        callBackPhoto = (photo) => {
           // I HAVE THE IMAGE URI HERE
        }

        renderCamera() {
           const isFocused = this.props.navigation.isFocused();

           if (!isFocused) {
               return null;
           } else if (isFocused) {
               return (
                  <CameraScreen callbackFromCamera={this.callBackPhoto}/>
               )
           }
        }

        render() {
           return (
             <View style={{ flex: 1 }}>
                {this.renderCamera()}
             </View>
          )
       }
  }

CameraScreen.js文件:

import React from "react";
import {
  StyleSheet,
  Text,
  View,
  Switch,
  TouchableOpacity,
  Image,
  Dimensions
} from "react-native";
import { Camera, Permissions } from "expo";

export default class CameraScreen extends React.Component {
  state = {
    hasCameraPermission: null,
    type: Camera.Constants.Type.back,
    imageuri: ""
  };

  async componentWillMount() {
    const { status } = await Permissions.askAsync(Permissions.CAMERA);
    this.setState({ hasCameraPermission: status === "granted" });
  }

  snap = async () => {
    if (this.camera) {
      let photo = await this.camera.takePictureAsync();
      if (photo) {
        this.setState({ imageuri: photo.uri });
      }
    }
  };

  render() {
    const { hasCameraPermission } = this.state;

    if (hasCameraPermission === null)
    {
      return <View />;
    }
    else if (hasCameraPermission === false)
    {
      return (
        <View>
          <Text>No Access to Camera</Text>
        </View>
      );
    }
    else
    {
      return (
        <View style={styles.container}>
            <View style={styles.cameraview}>
                {this.state.imageuri != "" ? (
                  this.props.callbackFromCamera(this.state.imageuri),
                  <Image
                    source={{uri: this.state.imageuri}}
                    style={styles.uploadedImage}
                    resizeMode="contain"
                  />
                ) : (
                  <Camera style={styles.camera} type={this.state.type} ref={ref => {this.camera = ref;}}>
                    <View style={styles.camerabuttonview}>
                      <TouchableOpacity style={styles.cameraButtons} onPress={this.snap}>
                        <Text style={{ fontSize: 18, marginBottom: 10, color: "white" }} >
                          Capture
                        </Text>
                      </TouchableOpacity>
                    </View>
                  </Camera>
                )}
            </View>
        </View>

      );
    }
  }
}

如何将图像uri从相机组件(或从CameraController.js)传递到onPress的相机标签标题按钮?

1 个答案:

答案 0 :(得分:0)

CameraController.js文件:

[4,5,4] and [8,9,9,8] only.

}