1

我在我的 react-native 应用程序中使用 react-native-camera 并且一切正常,但是当我拍照时,照片保存在我不想要的设备上,图像应该发送到 api 但不保存在我的设备上,有谁知道我怎样才能让它不保存在设备上?

render() {
return(
  <View style={styles.container}>
    <Camera
      style={styles.camera}
      ref={ref => {this.camera = ref;}}
      type={Camera.constants.Type.back}
      flashMode={Camera.constants.FlashMode.on}
      orientation={Camera.constants.Orientation.landscapeLeft}>
      <TouchableOpacity onPress={this.takePicture.bind(this)} style={{zIndex: 100}}>
        <View style={styles.cameraButton}></View>
      </TouchableOpacity>
    </Camera>
  </View>
  );
}

takePicture()
{
  this.camera.capture()
  .then((data) => this.sendData(data))
  .catch(err => console.error(err));
}
4

2 回答 2

3

正确答案是编辑#2!

您可以在我们的仓库中打开一个问题:https ://github.com/react-native-community/react-native-camera

图像保存在应用的缓存目录中。

当 promise 被解析时,该对象包含一个 uri,即图像的路径。或者您可以传递选项 base64: true,以从 takePictureAsync 承诺接收图像的 base64 表示。

使用 base64 字符串,您可以将其发送到您的服务器。这就是我们在应用程序中所做的。

编辑:如果您真的不希望它保存在应用程序的缓存目录中,我们可以在 takePictureAsync 方法中创建一个选项来执行此操作。随意在我们的 repo 中打开一个关于此的问题作为功能请求。

编辑#2:正确答案:您使用的是 RCTCamera,默认情况下将图像保存在设备上。

使用道具 captureTarget,传递值 Camera.constants.CaptureTarget.temp。所以:

<Camera
...
captureTarget={Camera.constants.CaptureTarget.temp}
...
</Camera>
于 2018-04-06T15:19:22.147 回答
1

你可以使用这个 API,我使用它,并且很容易发送图像。

https://github.com/react-community/react-native-image-picker

您避免了创建相机视图的工作,当您选择照片(或拍照)时,您会获得它的数据以发送到您想要的地方!

于 2018-04-06T14:45:28.403 回答