首页 > 解决方案 > 将图像 uri 转换为 Base64

问题描述

我使用下面的代码上传带有相机和画廊的图像。我想将其编码为 Base64 以离线保存。我看不到方法,如何修改我的代码以在 Base64 中转换 Image uri。

import PhotoUpload from 'react-native-photo-upload'

const [ image, setImage ] = useState( false )
//const [agree, setAgree] = useState(false);
const [ teacherNameActive, setTeachersNameActive ] = useState( false )


return (
    <>
        <LinearGradient
            colors={ [ '#FFFF', '#FFFF' ] }
            style={ styles.linearGradient }>
            <Text style={ styles.ScreenTitle }>Registration</Text>
            <ScrollView>
                <View style={ {
                    alignSelf: 'center',
                    backgroundColor: '#90C9C6',
                    width: 120,
                    height: 120,
                    borderRadius: 75
                } }>
                    <PhotoUpload onPhotoSelect={ Avatar => {
                        if ( Avatar ) {
                            console.warn( 'Image string:', Avatar )
                            setImage( true )
                        }
                    } }
                    >
                        { image ?
                            <Image
                                style={ styles.upload_Image_style }
                            /> : <>
                                <Image style={ styles.camera_image }
                                    source={ require( "../../assets/profileIcons/Combined_Shape.png" ) }
                                //source={ imageSource }
                                />
                                <Text style={ styles.camera_text }>Upload</Text>
                                <Text style={ styles.camera_text }>Photo</Text> 
                            </>
                        }
                    </PhotoUpload>
                </View>

标签: javascriptnode.jsreactjsreact-nativereact-hooks

解决方案


推荐阅读