Coder Social home page Coder Social logo

react-native-responsive-image's Introduction

React Native Responsive Image

David npm GitHub commits npm

Why?

React Native's Image size is rendered with the same dimensions regardless of device screen size and screen resolution. That's bad. This component scales itself seemlesly on all iOS and Android devices.

Installation

npm install react-native-responsive-image --save

Usage

<ResponsiveImage> is expecting initWidth and initHeight props.

These values are used to set image size on any device that has screen size iPhone Plus, or larger. Image is then scaled down for any smaller device.

Optional prop component is used to pass down either an Image or ImageBackground imported react-native (defaults to Image if not provided)

Example

import React, { Component } from "react";
import { AppRegistry, StyleSheet, View } from "react-native";
import ResponsiveImage from "react-native-responsive-image";

class App extends Component {
  render() {
    return (
      <View
        style={{
          flexGrow: 1,
          justifyContent: "center",
          alignItems: "center",
          flexDirection: "row"
        }}
      >
        <ResponsiveImage
          source={{ uri: "https://reactjs.org/logo-og.png" }}
          initWidth="138"
          initHeight="138"
        />
        <ResponsiveImage
          source={{ uri: "https://reactjs.org/logo-og.png" }}
          initWidth="138"
          initHeight="138"
        />
        <ResponsiveImage
          source={{ uri: "https://reactjs.org/logo-og.png" }}
          initWidth="138"
          initHeight="138"
        />
      </View>
    );
  }
}

AppRegistry.registerComponent("ResponsiveImageExample", () => App);

For initWidth=138 it looks like this:

Device Screen width Scale <Image> width
iPhone SE 320 0.77 106
iPhone X 375 0.902 117
iPhone8 Plus 414 1 138
Nokia 5 360 0.87 120
iPad (or any tablet) - 1 138

Just one image?

It sounds like you could save some loading by delivering low resolution images to screens with lower resolution. The best way is to serve just one high-resolution (retina) well compressed image. It’s surprising how well they can be compressed, and the result looks the same.

Retina Compression

Example project

Create project

expo init ResponsiveImageExample

  • go with 'expo-template-blank'

cp ./example/App.js ./ResponsiveImageExample/App.js

cp -R ./src ./ResponsiveImageExample

cd ResponsiveImageExample

yarn start

Development

  1. Modify any files in ./src directory

  2. Propagate changes by cp -R ./src ./ResponsiveImageExample/src

react-native-responsive-image's People

Contributors

ardmn avatar avishayil avatar bilalbudhani avatar blake-simpson avatar browniefed avatar chops318 avatar codybrouwers avatar corysimmons avatar cridenour avatar designorant avatar dreamniker avatar jayesbe avatar ladas-larry avatar mrova avatar neilsarkar avatar sf27 avatar taf2 avatar uqix avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.