使用 react-native-camera,如何访问保存的图片? [英] Using react-native-camera, how to access saved pictures?

查看:108
本文介绍了使用 react-native-camera,如何访问保存的图片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是使用 react-native-camera 并简单地在同一个屏幕上显示一张照片,如果已经拍了一张照片.我正在尝试将图片源保存为imageURI".如果它存在,我想显示它,如果还没有拍照,只显示文字说没有图像.我已经让相机工作了,因为我可以跟踪应用程序正在将图片保存到磁盘.遇到以下问题:

My goal is to use the react-native-camera and simply show a picture on the same screen, if a picture has been taken. I'm trying to save the picture source as "imageURI". If it exists, I want to show it, if a picture hasn't been taken yet, just show text saying No Image Yet. I've got the camera working, since I can trace the app is saving pictures to the disk. Having trouble with the following:

  • 如何在拍摄照片时将捕获函数数据分配给一个变量,以便稍后调用 (imageURI).
  • 不知道如何在 Javascript 中执行 if 语句来检查变量是否存在.

  • How to assign the capture functions data to a variable when I take the picture, that I can call later (imageURI).
  • Don't know how to do an if statement in Javascript to check if a variable exists yet.

import Camera from 'react-native-camera';

export default class camerahere extends Component {

_takePicture () {
  this.camera.capture((err, data) => {
  if (err) return;
  imageURI = data;
  });
}

render() {

  if ( typeof imageURI == undefined) {
    image = <Text> No Image Yet </Text> 
  } else {
    image = <Image source={{uri: imageURI, isStatic:true}} 
    style={{width: 100, height: 100}} />
  }


return (
  <View style={styles.container}>
    <Camera
      captureTarget={Camera.constants.CaptureTarget.disk}
      ref={(cam) => {
        this.camera = cam;
      }}
      style={styles.preview}
      aspect={Camera.constants.Aspect.fill}>

      {button}
    <TouchableHighlight onPress={this._takePicture.bind(this)}>
      <View style={{height:50,width:50,backgroundColor:"pink"}}></View>
</TouchableHighlight>
</Camera>

推荐答案

我找到了自己问题的答案.这是使用 react-native-camera 的示例.
https://github.com/spencercarli/react-native-snapchat-c​​lone/blob/master/app/routes/Camera.js

I found the answer to my own question. This is an example of the react-native-camera being used.
https://github.com/spencercarli/react-native-snapchat-clone/blob/master/app/routes/Camera.js

在@vinayr 回答的另一个早先发布的问题中找到了这个答案.谢谢!在图像上从相机获取最近点击的图像在本机中查看

Found this answer in another earlier posted question answered by @vinayr. Thanks! Get recently clicked image from camera on image view in react-native

这是第一个链接中的代码:

Here's the code from the first link:

import React, { Component } from 'react';
import {
  View,
  StyleSheet,
  Dimensions,
  TouchableHighlight,
  Image,
  Text,
} from 'react-native';
import Camera from 'react-native-camera';

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',
    backgroundColor: '#000',
  },
  preview: {
    flex: 1,
    justifyContent: 'flex-end',
    alignItems: 'center',
    height: Dimensions.get('window').height,
    width: Dimensions.get('window').width
  },
  capture: {
    width: 70,
    height: 70,
    borderRadius: 35,
    borderWidth: 5,
    borderColor: '#FFF',
    marginBottom: 15,
  },
  cancel: {
    position: 'absolute',
    right: 20,
    top: 20,
    backgroundColor: 'transparent',
    color: '#FFF',
    fontWeight: '600',
    fontSize: 17,
  }
});

class CameraRoute extends Component {
  constructor(props) {
    super(props);

    this.state = {
      path: null,
    };
  }

  takePicture() {
    this.camera.capture()
      .then((data) => {
        console.log(data);
        this.setState({ path: data.path })
      })
      .catch(err => console.error(err));
  }

  renderCamera() {
    return (
      <Camera
        ref={(cam) => {
          this.camera = cam;
        }}
        style={styles.preview}
        aspect={Camera.constants.Aspect.fill}
        captureTarget={Camera.constants.CaptureTarget.disk}
      >
        <TouchableHighlight
          style={styles.capture}
          onPress={this.takePicture.bind(this)}
          underlayColor="rgba(255, 255, 255, 0.5)"
        >
          <View />
        </TouchableHighlight>
      </Camera>
    );
  }

  renderImage() {
    return (
      <View>
        <Image
          source={{ uri: this.state.path }}
          style={styles.preview}
        />
        <Text
          style={styles.cancel}
          onPress={() => this.setState({ path: null })}
        >Cancel
        </Text>
      </View>
    );
  }

  render() {
    return (
      <View style={styles.container}>
        {this.state.path ? this.renderImage() : this.renderCamera()}
      </View>
    );
  }
};

export default CameraRoute;

这篇关于使用 react-native-camera,如何访问保存的图片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆