1

我正在尝试集成react-native-image-picker并且我能够成功安装它。现在,当我尝试从日志中的图库中选择图像时,它确实显示我的 imageSource 具有价值但它不显示。我目前正在 iOS 模拟器上运行它。下面是我的代码

import ImagePicker from "react-native-image-picker";

const options = {
  title: "Select a photo",
  takePhotoButtonTitle: "Take a photo",
  chooseFromLibraryButtonTitle: "Choose from gallery",
  quality: 1
};

constructor(props) {
    super(props);
    this.state = { imageSource: null };
  }

addImage = () => {
ImagePicker.showImagePicker(options, (response) => {
  console.log('Response = ', response);

  if (response.didCancel) {
    console.log('User cancelled image picker');
  } else if (response.error) {
    console.log('ImagePicker Error: ', response.error);
  } else {
    const source = { uri: response.uri };

    this.setState({
      imageSource: source,
    });
    console.log("Imagesource=" + JSON.stringify(source));
  }
});
}

render() {
    return (
      <View style={styles.container}>
        <Image source={this.state.imageSource} />
      </View>
    );
  }

控制台日志确实打印了我从图库中选择的图像的路径,但它只是不显示。我在 iPad 上横向运行它。任何帮助表示赞赏。

4

1 回答 1

2

显示的任何图像都应具有固定widthheight.

您可以使用Dimension类似这样var { width, height } = Dimensions.get("window");的方法来使用相对于屏幕尺寸的高度和宽度(不要忘记从 导入react-native)。

或者您可以继续为相应的参数赋予您自己的值。

于 2019-01-03T09:50:45.120 回答