PixelRatio and Image Resolution

+15 Mana ✨

React Native lays out views in density independent units, while the screen draws physical pixels, so on a device with a pixel ratio of 3 a 100 unit box spans 300 pixels. Bundled images get the right file through their @2x and @3x variants, but a remote image is whatever size the server sends. Converting layout sizes to pixels, or offering the server's files at several densities, lets each device get a remote image that matches its screen density.

Key terms

  • PixelRatio: the module that gives access to the device's pixel density and font scale.
  • dp and px: dp is the layout unit React Native sizes views in, and px is a physical pixel on the screen.
  • PixelRatio.get(): a method that returns the device pixel density as a number, such as 2 or 3.
  • srcSet: an Image prop holding a comma separated list of image URLs, each followed by a pixel density descriptor such as 2x.

Pixel density in numbers

PixelRatio.get() takes no arguments and returns the scale of the current window. Typical values are 1 on mdpi Android devices, 1.5 on hdpi, 2 on xhdpi Android devices and phones such as the iPhone 11, 3 on xxhdpi devices, the Pixel and the iPhone X and XS, and 3.5 on xxxhdpi devices.

Ask for physical pixels

A good rule of thumb for remote images is to multiply the size you display by the pixel ratio. PixelRatio.getPixelSizeForLayoutSize(layoutSize) does that multiplication for you and rounds the product with Math.round, so the result is always an integer. At a ratio of 3, a 64 dp avatar becomes 192 px. On an hdpi device at 1.5, 75 dp gives 112.5, which rounds to 113.

The pixel size belongs in the request to your image server, not in the layout. The Avatar component below asks for a 192 px file on a 3x phone but still lays the Image out at 64 by 64:

tsx
const pixels = PixelRatio.getPixelSizeForLayoutSize(AVATAR_SIZE);

styles.avatar keeps width and height at AVATAR_SIZE. Passing pixels there instead would make the avatar three times larger on that phone.

Snapping a computed size to whole pixels

Inside JavaScript and the layout engine, React Native works with arbitrary precision numbers. It rounds only when it sets the position and size of the native views, and it rounds relative to the root rather than the parent so rounding errors don't pile up.

When you compute sizes yourself, PixelRatio.roundToNearestPixel(layoutSize) rounds a layout size to the nearest value that corresponds to a whole number of pixels. It returns a layout size, not a pixel count: it computes Math.round(layoutSize * ratio) / ratio. At a ratio of 2, a 100 dp row split into thirds gives 33.33 dp, or 66.67 px. PixelRatio.roundToNearestPixel(100 / 3) returns 33.5, which is exactly 67 px.

Round all the related values or none of them. Mixing rounded and unrounded values accumulates rounding errors, and a single error can make a one pixel border vanish or come out twice as thick.

Several files for several densities

When the server publishes fixed files at a few resolutions, list them in srcSet instead of computing a size. Each comma separated candidate is a URL followed by a density descriptor, and a candidate with no descriptor counts as 1x:

tsx
<Image
  srcSet={`${cdn}/cover.jpg, ${cdn}/cover@2x.jpg 2x, ${cdn}/cover@3x.jpg 3x`}
  width={320}
  height={180}
  style={{width: 320, height: 180}}
/>

React Native turns each candidate into a source that carries the candidate's scale plus the width and height props. With several sources to choose from, the native side picks the best one for the measured size of the image container. Those props don't size the view, though. When there is more than one candidate, Image builds its layout style from style alone, so the element above also sets style={{width: 320, height: 180}} to give the frame its size.

React Native applies three rules when it reads the value:

  • Only x descriptors work. A width descriptor such as 300w logs a warning and that candidate is skipped. If no candidate is valid, React Native warns that the srcSet value is not valid.
  • When srcSet has no 1x candidate, the src prop, a string with a remote URL, is used as the 1x image if you provide it.
  • srcSet takes precedence over both src and source, and src in turn takes precedence over source.

Code Examples

tsx
import {Image, PixelRatio, StyleSheet} from 'react-native';
// The app's own helper that builds an image server URL for a given pixel size
import {avatarUrl} from './imageServer';

const AVATAR_SIZE = 64;

type AvatarProps = {
  userId: string;
};

export default function Avatar({userId}: AvatarProps) {
  // 192 on a device where PixelRatio.get() returns 3
  const pixels = PixelRatio.getPixelSizeForLayoutSize(AVATAR_SIZE);

  return (
    <Image source={{uri: avatarUrl(userId, pixels)}} style={styles.avatar} />
  );
}

const styles = StyleSheet.create({
  // Layout stays in dp: only the request uses the pixel size
  avatar: {
    width: AVATAR_SIZE,
    height: AVATAR_SIZE,
  },
});
✓ Completed