React Native - How to load local image using the uri in Image Component?

前端 未结 3 1164
慢半拍i
慢半拍i 2021-02-06 03:34

I know we can load the local image with:


But i need to load the

3条回答
  •  攒了一身酷
    2021-02-06 04:24

    These are the three ways to render images in react-native , In your case you can encode the image

    • you can require image

      source={require('/react-native/img/favicon.png')}

    • you can get image from web

      source={{uri: 'https://facebook.github.io/react-native/docs/assets/favicon.png'}}

    • or you can encode the image

      source={{uri: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADMAAAAzCAYAAAA6oTAqAAAAEXRFWHRTb2Z0d2FyZQBwbmdjcnVzaEB1SfMAAABQSURBVGje7dSxCQBACARB+2/ab8BEeQNhFi6WSYzYLYudDQYGBgYGBgYGBgYGBgYGBgZmcvDqYGBgmhivGQYGBgYGBgYGBgYGBgYGBgbmQw+P/eMrC5UTVAAAAABJRU5ErkJggg=='}}

    as doc suggested as below

    export default class DisplayAnImage extends Component {
      render() {
        return (
          
            
            
            
          
        );
      }
    }
    

    or you can create a json file that contains the encoded string of the image image.json

    {imageString: '64encodedString'}
    

    them import the file

    import image from 'image.json';
    

    then image

    
    

提交回复
热议问题