Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

212
Visualizações
base64 images sometimes don't load in react native

I'm trying to load a base64 image from my backend api, however I've reloaded my code a few times without changing anything, and sometimes it works just fine but most of the time it doesn't

enter code here

my code :

const [image, setImage] = useState('');

    useEffect(() => {
        fetch('http://example:8000/api/auth/return_picture/image',{
            method: 'GET'})
            .then((response) => response.json())
            .then((responseJson) => {
                setImage(responseJson.uri);
            })
            .catch((error) => {
                console.log(error);
            });
    }, []);

return(
        <View style={{flex: 1, alignItems: 'center', justifyContent: 'center'}}>
            <Image
                source={{uri: image ? `data:image/jpg;base64,${image}` : "https://i.pinimg.com/236x/09/f0/84/09f084cd8a8093ea5738a3ab75c210df.jpg"}}
                style={styles.avatar}/>
        </View>
    );

backend (php) :

/**
     * @Route("/return_picture/{image}", name="return_picture", methods={"GET"})
     */
    public function return_picture(ManagerRegistry $doctrine, Request $request, $image): Response
    {

        $file = "uploads/profile_pictures/" . $image . ".jpg";
        $test = file_get_contents($file);


        return $this->json([
           "uri" => base64_encode($test)
        ]);
    }

sometimes it shows the proper image that i want to display : properly working

sometimes it only loads halfway : halfway there

sometimes it doesn't work at all and doesn't even show the default picture : not working

sometimes it shows the default picture and the console shows an error message that says JSON Parse error: Expected '}' (this happens most of the time)

any help would be much appreciated c-: !

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda