83

The documentation says that the only way to reference a static image is to use require.

But I'm not sure where does react expect to have those images. The examples don't have any domain, so it seems like you have to go to Xcode and add them to Images.xcassets, but that didn't work for me.

1
  • To be clear, are you asking for how to load images that are included in the application? Commented Feb 24, 2022 at 17:58

13 Answers 13

93

Using React Native 0.41 (in March 2017), targeting iOS, I just found it as easy as:

<Image source={require('./myimage.png')} /> 

The image file must exist in the same folder as the .js file requiring it for "./" to work.

I didn't have to change anything in the XCode project. It just worked.

Note that the path seems to have to start with "./" or "../" and be full lower case. I'm not sure what all the restrictions are, but start simple and work forward.

Hope this helps someone, as many other answers here seem overly complex and full of (naughty) off-site links.

UPDATE: BTW - The official documentation for this is here: https://reactnative.dev/docs/images

Sign up to request clarification or add additional context in comments.

2 Comments

Even the official documentation says - i can make a folder is named img and then i can put all the images into that folder. It doesn't work for me ! I must put all the images on the same folder where the .js is. Strange !!!
This most definitely helped me. My problem was I was putting the images folder OUTSIDE the program folder
32

ES6 solution:

import DefaultImage from '../assets/image.png'; const DEFAULT_IMAGE = Image.resolveAssetSource(DefaultImage).uri; 

and then:

<Image source={{uri: DEFAULT_IMAGE}} /> 

Comments

22

It works exactly as you expect it to work. There's a bug https://github.com/facebook/react-native/issues/282 that prevents it from working correctly.

If you have node_modules (with react_native) in the same folder as the xcode project, you can edit node_modules/react-native/packager/packager.js and make this change: https://github.com/facebook/react-native/pull/286/files . It'll work magically :)

If your react_native is installed somewhere else and the patch doesn't work, comment on https://github.com/facebook/react-native/issues/282 to let them know about your setup.

2 Comments

Thanks. I would up vote your answer but I'm still too new in StackExchange. Anyway, marked as good :)
You shouldn't have to edit the file anymore. The fix seems to be to drag/drop the images to the assets bundle and keep the name the same as the filename on disk.
9

If loading images dynamically one can create a .js file like following and do require in it.

export const data = [ { id: "1", text: "blablabla1", imageLink: require('../assets/first-image.png') }, { id: "2", text: "blablabla2", imageLink: require('../assets/second-image.png') } ] 

In your component .js file

import {data} from './js-u-created-above'; ... function UsageExample({item}) { <View> <Image style={...} source={item.imageLink} /> </View> } function ComponentName() { const elements = data.map(item => <UsageExample key={item.id} item={item}/> ); return (...); } 

Comments

6

I had this exact same issue until I realized I hadn't put the image in my Image.xcassets. I was able to drag and drop it into Xcode with Image.xcassets open and after rebuilding, it fixed the problem!

enter image description here

1 Comment

What if we want to access an image from 'custom.xcassets' ? Can you help me out here stackoverflow.com/questions/39203388/…
6

To display image from local folder, you need to write down code:

<Image source={require('../assets/self.png')}/> 

Here I have put my image in assets folder.

Comments

5

From the UIExplorer sample app:

Static assets should be required by prefixing with image! and are located in the app bundle.

enter image description here

So like this:

render: function() { return ( <View style={styles.horizontal}> <Image source={require('image!uie_thumb_normal')} style={styles.icon} /> <Image source={require('image!uie_thumb_selected')} style={styles.icon} /> <Image source={require('image!uie_comment_normal')} style={styles.icon} /> <Image source={require('image!uie_comment_highlighted')} style={styles.icon} /> </View> ); } 

3 Comments

Oh! I didn't notice this example with images. Thanks for pointing it. Still, I haven't been able to make it work on my test project. Will continue trying later and post my result
Where do you place the image in the project? I tried adding it in the Images.xcassets by dragging it into Xcode, but just get 'unknown module "image!backdrop". The code is here: gist.github.com/StefanWallin/3abbb026c03615545bd8
So in the end my issue was related to the packages. This answer helped fix it.
5

I was having trouble with react-native-navigation, I created my own header component, then inserted a image - as logo - on the left before title, then when I was triggering navigate to another screen and then back again, logo was loading again, with a timeout near 1s, my file were local. My solution :

Logo.json

{"file" : "base64 big string"} 

App.js

import Logo from '.../Logo.json' ... <Image source={{uri:Logo.file}} /> 

1 Comment

Should be <Image source={{uri:Logo.file}} />
4

We can do like below:

const item= { image: require("../../assets/dashboard/project1.jpeg"), location: "Chennai", status: 1, projectId: 1 } <Image source={item.image} style={[{ width: 150, height: 150}]} /> 

Comments

0

This from https://github.com/facebook/react-native/issues/282#issuecomment-155901680 worked for me:

It should be mentioned that you don't have to put the images in Images.xcassets - you just put them in the project root and then just require('./myimage.png') as @anback wrote

Look at this SO answer and the pull it references

Comments

0

For typescript user

import { ImageSourcePropType } from 'react-native' type Data = { image:ImageSourcePropType } const data:Data = { image:require('../.../log.png') } and then <Image source={data.image}/> 

Comments

0

import { View, Text, Image, ScrollView, TextInput } from 'react-native';

 //both will be work <Image source={require('./icon.png')} /> //or <Image source={{uri: 'https://reactnative.dev/docs/assets/p_cat2.png'}} /> 

Comments

-19

You have to add to the source property an object with a property called "uri" where you can specify the path of your image as you can see in the following example:

<Image style={styles.image} source={{uri: "http://www.mysyte.com/myimage.jpg"}} /> 

remember then to set the width and height via the style property:

var styles = StyleSheet.create({ image:{ width: 360, height: 40, } }); 

1 Comment

I think OP was looking for how to do static images, instead of network-loaded ones.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.