Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

208
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!