Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Core Components  ›  Lesson

Image and ImageBackground

Image 12 minLocal images are bundled with require and a fixed path, remote images use a uri and must be given a width and height, and ImageBackground draws children on top of a picture.

Image and ImageBackground

A plant list without photos looks like a spreadsheet. Sprout shows a small photo on every card and a leafy header behind the screen title. Images on a phone have one rule web developers often miss: the app must know how big a remote image is before it arrives.

The rule

Local images use require; remote images need a uri and a size.

There are two kinds of source:

import { Image } from 'react-native';

// Bundled with the app
<Image source={require('./assets/fern.png')} />

// Downloaded from the internet
<Image
  source={{ uri: 'https://example.com/photos/fern.jpg' }}
  style={{ width: 64, height: 64, borderRadius: 8 }}
  resizeMode="cover"
/>
  • Local images use require with a fixed path. Metro packs the file into the app and knows its size, so it can show it without extra styling. The path must be a literal string; require('./assets/' + name) does not work, because Metro reads requires while bundling.
  • Remote images use an object with uri. React Native cannot know their size in advance, so you must set width and height in the style, or the image renders at zero size and is invisible.
  • resizeMode decides how the picture fits its box: 'cover' fills and crops, 'contain' shows it whole with gaps, 'stretch' distorts to fit.
  • alt gives screen readers a description.

ImageBackground is an Image that can hold children, for text over a picture:

<ImageBackground
  source={require('./assets/leaves.jpg')}
  style={{ height: 160, justifyContent: 'flex-end' }}
>
  <Text style={{ color: 'white', fontSize: 24 }}>My plants</Text>
</ImageBackground>

It also needs a size from its style. Its children sit on top of the image.

The shortcut

For any image, ask "is the file in my project or on the internet?" In the project: require with a fixed path. On the internet: { uri } plus width and height.

The plant photo on a cardStep 1 of 4
  1. Where is the file?

    Users take photos, and they are uploaded to a server. On the internet.

Drills

Answer fast — trust the rule.

quick drill

A remote Image with { uri } and no style shows nothing. Why?

quick drill

Which is the correct way to show assets/cactus.png from the project?

quick drill

Which resizeMode shows the whole photo, leaving empty space if the shapes differ?

quick drill

Why does require('./assets/' + plant.file) fail?

quick drill

You want the title "My plants" drawn on top of a photo. Which component fits?

Remember

  • Local images use require; remote images need a uri and a size.
  • Ask "in my project or on the internet?"
  • Trap: a remote image without width and height is invisible.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainLocal or remote?

Sprout shows a bundled placeholder leaf when a plant has no photo, and the user's uploaded photo otherwise. Explain how you would write the Image source and style for both cases.