简体   繁体   English

<img>即使在使用 react-native-vision-camera 拍摄新照片后也能查看旧照片

[英]<Image> view the old picture even after capturing a new picture using react-native-vision-camera

I'm using react-native-vision-camera to capture a picture and view it inside an <Image> component.我正在使用react-native-vision-camera捕捉图片并在<Image>组件中查看它。 The expected behavior is updating the picture after capturing a new one.预期的行为是在捕获新图片后更新图片。 Since the source property of the <Image> component points to a new picture (with the same name Ok, but still, it's a new picture).由于<Image>组件的source属性指向一张新图片(同名好吧,但它仍然是一张新图片)。

But this never happens.但这永远不会发生。 Even when I reload the app, <Image> still shows the old picture.即使我重新加载应用程序, <Image>仍然显示旧图片。 I have to rename the new picture.我必须重命名新图片。

I thought that this happens because maybe the state of the source property updates before react-native-vision-camera saves the new picture so the <Image> component renders the old picture but I'm using async-await so as far as I know, this shouldn't happen.我认为发生这种情况是因为在react-native-vision-camera保存新图片之前source属性的 state 可能会更新,因此<Image>组件呈现旧图片但据我所知我正在使用async-await ,这不应该发生。

The code I wrote for the capturing function is我为捕获 function 编写的代码是

const [photoPath, setPhotoPath] = useState("");

const capture = async () => {
        const photo = await camera.current.takePhoto();
        const path = RNFS.ExternalDirectoryPath + '/photo-X.jpg';
        //when i update X (in the saved image name) with a new number,
        //<Image> views the correct image.

        await RNFS.moveFile(photo.path, path)
        setPhotoPath("file://" + path)
    }

the code to view the image查看图片的代码

{
    photoPath.length > 0 ?
        < Image
          source={{ uri: photoPath }}
          style={[styles.camera, { backgroundColor: 'black' }]}
        />
    :
        null
}

I have a capture <Button> that calls the capture() function that updates the photoPath state that should update the source of the <Image> component and this process should view the new picture.我有一个捕获<Button>调用capture() function 更新photoPath state 应该更新<Image>组件的source并且这个过程应该查看新图片。 But this never happens even after reloading the app.但这即使在重新加载应用程序后也不会发生。 I have to pick a new name for the new picture to get it to be viewed.我必须为新图片选择一个新名称才能查看它。 Why I'm getting this behavior instead of the one I'm expecting?为什么我得到这种行为而不是我期望的行为? What's wrong with my code, or my understanding, and what is the correct solution?我的代码或我的理解有什么问题,正确的解决方案是什么?

After a day of searching and debugging, I found out that <Image> for a reason that I don't know, renders the cashed image.经过一天的搜索和调试,我发现<Image>出于我不知道的原因渲染了兑现的图像。 And since there is no easy way to delete cash in react-native, this is a work-around that solved my problem.由于没有简单的方法可以删除 react-native 中的现金,因此这是解决我问题的变通方法。

< Image
    source={{ uri: photoPath + "?" + new Date() }}
    style={[styles.camera, { backgroundColor: 'black' }]}
/>

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

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