【问题标题】:Image is not shown after image is selected from react-native image-picker从 react-native image-picker 中选择图像后不显示图像
【发布时间】:2021-12-15 06:18:05
【问题描述】:

我正在开发一个应用程序,我需要从手机摄像头和图库中上传图像。相机和图库未在图像标签内显示图像。

这是我的代码

function profile({ route,navigation }) {
    const [imageUri, setimageUri] = useState('');



    const openCamera = () => {
        let options={
            storageOptions: {
                path: 'images',
                mediaType: 'photo',
            },
        };
        launchCamera(options, response => {
            console.log("response =", response);
            if(response.didCancel){
                console.log('user cancelled image picker');
            }else if(response.error){
                console.log('Image picker Error:', response.error);
            }else if(response.customButton){
                console.log('User taped cutom button:', response.customButton);
            }else{
                const source = {uri: 'data: image/jpeg;base64,'+ response.base64 };
                setimageUri(source);    
            }
        });
    }

这是图像视图代码

<Image
  source={imageUri}
  style={{
    height: 100,
    width: 100,
    borderRadius: 100,
    borderWidth: 2,
    borderColor: 'black',
    alignSelf: 'center',
  }}
/>  

【问题讨论】:

    标签: react-native react-native-image-picker


    【解决方案1】:

    试试这个,对你有帮助

    function profile({ route,navigation }) {
        const [imageUri, setimageUri] = useState('');
    
        const openCamera = () => {
            let options={
                storageOptions: {
                    path: 'images',
                    mediaType: 'photo',
                },
            };
            launchCamera(options, response => {
                console.log("response =", response);
                if(response.didCancel){
                    console.log('user cancelled image picker');
                }else if(response.error){
                    console.log('Image picker Error:', response.error);
                }else if(response.customButton){
                    console.log('User taped cutom button:', response.customButton);
                }else{
                    setimageUri(response.assets[0].uri);    
                }
            });
        }
    
    
    <Image
      source={{ uri: imageUri }}
      style={{
        height: 100,
        width: 100,
        borderRadius: 100,
        borderWidth: 2,
        borderColor: 'black',
        alignSelf: 'center',
      }}
    />  
    

    response 将为您提供assets 数组,您可以将图像uri 作为response.assets[0].uri 访问。

    【讨论】:

    • 感谢您的回答,但仍然无法收到“失败的道具类型:无效的道具source 提供给image。”此警告。
    • 我认为这种格式添加 require 是正确的,但出现错误“invalid call at line”
    • 检查我编辑的答案。它现在可以工作了。在图片源中使用uri
    • 感谢哥们这次成功了,你的回答对我帮助很大。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-29
    • 1970-01-01
    • 2023-01-31
    • 2019-07-16
    • 1970-01-01
    相关资源
    最近更新 更多