Rajat Verma
Rajat Verma

Reputation: 460

How to write local image file path as a uri path

I want to convert the local image file path to the uri path so that in place of uri where a website is require i want to put the local image directory. I am showing the code below..

state = {
        search: '',
        images: [
            '../../assets/assets/img/formula.png',  //***this  is not working
            'https://source.unsplash.com/1024x768/?water',
            'https://source.unsplash.com/1024x768/?girl',
            'https://source.unsplash.com/1024x768/?tree'
          ]
      };


//Image is used here..

<SliderBox images={this.state.images} />

In image array i want the first image must include from my local file image. Please someone tell how to do that so that in image I can include my local image file path

Upvotes: 0

Views: 1749

Answers (2)

Bur
Bur

Reputation: 157

A bit late but hopefully it can help someone. You can use resolveAssetSource from Image, so going back to your code it would look like this,

state = {
    search: '',
    images: [
        Image.resolveAssetSource(require("../../assets/assets/img/formula.png")).uri,
        'https://source.unsplash.com/1024x768/?water',
        'https://source.unsplash.com/1024x768/?girl',
        'https://source.unsplash.com/1024x768/?tree'
      ]
  };

Of course you would need to import Image first,

import { Image } from "react-native";

Upvotes: 1

Mauricio Carlezzo
Mauricio Carlezzo

Reputation: 140

You need to import Ex..:

import Formula from '../../assets/assets/img/formula.png';

Then you can use Formula as an image! You can use require('../../assets/assets/img/formula.png') too.

Upvotes: 1

Related Questions