web-dev-qa-db-ja.com

反応ネイティブカメラを使用して、保存された画像にアクセスする方法

私の目標は、react-native-cameraを使用して、写真が撮られている場合は、同じ画面に写真を表示することです。画像ソースを「imageURI」として保存しようとしています。存在する場合は、それを表示します。写真がまだ撮影されていない場合は、「画像がありません」というテキストを表示します。アプリが画像をディスクに保存しているのを追跡できるので、カメラは機能しています。次の問題:

  • 写真を撮るときにキャプチャ関数データを変数に割り当てる方法。後で呼び出すことができます(imageURI)。
  • Javascriptでifステートメントを実行して、変数がまだ存在するかどうかを確認する方法がわかりません。

    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>
    
9
Arsalan S.

自分の質問に対する答えを見つけました。これは、使用されている反応ネイティブカメラの例です。
https://github.com/spencercarli/react-native-snapchat-c​​lone/blob/master/app/routes/Camera.js

この回答は、@ vinayrが回答した以前に投稿された別の質問で見つかりました。ありがとう! 反応ネイティブの画像ビューでカメラから最近クリックした画像を取得

最初のリンクのコードは次のとおりです。

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;
11
Arsalan S.