React Native setState在captureScreen函数中不起作用

时间:2018-01-02 12:41:43

标签: react-native screenshot state

我使用此功能捕获屏幕截图。我需要存储uri但在此函数中setState不起作用。调用takeSnap()函数后发现this.state.imageUri为空。不明白为什么?我该如何解决这个问题?

takeSnap() {
    var albumName="ShilpaMela";
    captureScreen({
      format: "jpg",
      quality: 0.8
    })
    .then(
      uri => {
        this.setState({ imageUri: uri })
        CameraRollExtended.saveToCameraRoll({
          uri: uri,
          album: albumName
        }, 'photo')
      },
      error => console.error("Oops, snapshot failed", error)
    );
  }

1 个答案:

答案 0 :(得分:0)

更多代码

handleEmail = () => {
    console.log(3);
    Mailer.mail({
      subject: image,
      recipients: ['support@example.com'],
      ccRecipients: ['supportCC@example.com'],
      bccRecipients: ['supportBCC@example.com'],
      //body: '<b>A Bold Body </b>',
      body: "Body",
      isHTML: true,
      attachment: {
        path: this.state.imageUri,  // The absolute path of the file from which to read data.
        type: 'jpg',   // Mime Type: jpg, png, doc, ppt, html, pdf
        name: '',   // Optional: Custom filename for attachment
      }
    }, (error, event) => {
      Alert.alert(
        error,
        event,
        [
          {text: 'Ok', onPress: () => console.log('OK: Email Error Response')},
          {text: 'Cancel', onPress: () => console.log('CANCEL: Email Error Response')}
        ],
        { cancelable: true }
      )
    });
  }

_handlePressScreen = () => {
    let camera = frontCamera 
    if (this.state.camera === camera) {
      camera = backCamera
    }
    this.setState({ camera })
  }

  renderCamera() {
    if(snapFlag==1){
      sub="help"
      //this.takeSnap.bind(this);
      this.takeSnap();
      this.handleEmail();
    }
    console.log(4);
    snapFlag=0;
    return (
      <TouchableWithoutFeedback style={styles.flex} onPress={this._handlePressScreen}>
        <View style={[styles.flex]}>
      <Camera
        ref={(cam) => {
          this.camera = cam;
        }}
        type={this.state.camera}
        style={styles.preview}
        aspect={Camera.constants.Aspect.fill}
        //captureTarget={Camera.constants.CaptureTarget.disk}
        captureTarget={Camera.constants.CaptureTarget.temp}
      >
      	<Image
              style={{width: 400, height: 400}}
              source={require('./image/shilpamela.png')}
            />
        <TouchableHighlight
          style={styles.capture}
          onPress={this.takePicture.bind(this)}
          underlayColor="rgba(255, 255, 255, 0.5)"
        >
          <View>
            <Image
              style={{width: 60, height: 60}}
              source={require('./image/cam_button.png')}
            />
          </View>
        </TouchableHighlight>
      </Camera>
      </View>
      </TouchableWithoutFeedback>
    );
  }

相关问题