react-native - 在 react-native 的 ImageView 中从相机获取最近点击的图像

标签 react-native react-native-android react-native-camera

我正在我的 native 应用程序中实现相机。单击后,我想在同一屏幕上的小 ImageView 中显示该相机最近捕获的图像。但我不知道如何访问最近点击的图像。每当通过电话点击任何新图像时,它都会被赋予一个默认的随机名称,我如何在 ImageView 的源中引用它?请建议我这样做。我的代码是

 class Incident extends Component {
    constructor(props)
    {  super(props)

        this.state=  {path:"",show:false,video:Camera.constants.CaptureMode.video,camera:Camera.constants.CaptureMode.camera}    
      }
    render() {
      return (
              <View style={styles.container}>
             <View style={{flex:72}}>
             <Camera
              ref={(cam) => {
               this.camera = cam;
                }}
              style={styles.preview}
               aspect={Camera.constants.Aspect.fill}
              captureMode={this.state.camera}>

          </Camera>

  </View>
  <View style={{flex:8,opacity:0.5,backgroundColor:'black'}}>

    {()=>this.showClickedImage()}

  </View>


  <View style={{flex:10,flexDirection:'row',justifyContent:'space-between',backgroundColor:'#f3396b'}}>
  <TouchableOpacity style={{flex:33,flexDirection:'column', alignItems: 'center',paddingTop:10}}>
  <Image style={styles.icon} source={require('./Images/menu.png')}/>
  <Text>Home</Text>
  </TouchableOpacity>



  <TouchableOpacity style={{flex:34,flexDirection:'column', height:70,alignItems: 'center',borderColor:'#dd1e52',borderWidth:1,paddingTop:5}} 
                               onPress={()=>this.takePicture()}>
  <Image style={{width:50,height:50}} source={require('./Images/capture.png')}/>

  </TouchableOpacity>





  <TouchableOpacity style={{flex:33,flexDirection:'column', alignItems: 'center',paddingTop:10}}>
  <Image style={styles.icon} source={require('./Images/profile.png')}/>
  <Text>Profile</Text>
  </TouchableOpacity>


  </View>
  </View>
);
      }

      showClickedImage()
  { 
        return <View style={{flex:1,height:40,width:40, borderWidth:1,borderColor:'black'}}>
               <Image style={{width:40,height:40}} source= {{uri: this.state.path}}/>
               </View>
               }

       takePicture() 
     {

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

最佳答案

camera.capture()返回捕获图像的路径。用它来显示 Image成分。

<Image source={{uri: data.path}} style={{width: 100, height: 100}} />

关于react-native - 在 react-native 的 ImageView 中从相机获取最近点击的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40583159/

相关文章:

react-native - 如何在 Expo 中使用 DocumentPicker 选择多个文件?

react-native - 如何在选项卡导航器 react 中嵌套包装堆栈导航器的组件

react-native - 创建充满导出 react-native 的索引文件

react-native - 开发人员菜单中缺少远程调试 JS 选项

react-native - 我如何在React Native中一次请求多个权限

react-native - React 0.18 导致模块错误

react-native - 如何使用 react-native-camera 显示来自相机胶卷 url 的图像?

ios - 在 AppCenter 上构建的 iOS 上找不到相关 ID 错误

react-native-camera - 如何在相机中添加边框?

react-native - 无法在我的项目上运行 react-native 链接