简体   繁体   English

反应本机图像选择器无法显示图像

[英]React Native Image Picker Unable To Display Image

i'm trying to use image picker and display image on Image component.我正在尝试使用图像选择器并在图像组件上显示图像。 Here my code这是我的代码

const [photo, setPhoto] = useState("")

const options = {
    title: "pick an image",
    storageOptions: {
      skinBackup: true,
      path: 'images'
    }
}

const openCamera = async _ => {
await launchCamera(options,  (res) => {
  if (res.didCancel) {
    console.log("canceled")
  }
  else if (res.error) {
    console.log("err")
  }
  else if (res.customButton) {
    console.log("cstm btn")
  }
  else {
    const source =  { uri:  res.uri };
    console.log("Imagesource=" + JSON.stringify(source));
  }
})

} }

here Image component这里图像组件

<ScrollView style={styles.postContent}>
  {
    photo && (
        <Image
            source={photo}
            style={{ height: 300, width: 300 }} />
        )
  }

camera can open and take a photo.相机可以打开并拍照。 then i can't see the image source like this.然后我看不到这样的图像源。

Imagesource={}

DO this please请这样做

const [photo, setPhoto] = useState("")

const options = {
    title: "pick an image",
    storageOptions: {
      skinBackup: true,
      path: 'images'
    }
}

const openCamera = async _ => {
await launchCamera(options,  (res) => {
  if (res.didCancel) {
    console.log("canceled")
  }
  else if (res.error) {
    console.log("err")
  }
  else if (res.customButton) {
    console.log("cstm btn")
  }
  else {
    setPhoto(res?.uri)
  }
})

And in here do this:在这里这样做:

<ScrollView style={styles.postContent}>
  {
    photo && (
        <Image
            source={{uri:photo}}
            style={{ height: 300, width: 300 }} />
        )
  }

Hope it helps希望能帮助到你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM