Does react native support base64 encoded images?

前端 未结 4 2268
庸人自扰
庸人自扰 2020-12-14 14:31

Does react native support base 64 encoded images?

I tried:



        
相关标签:
4条回答
  • 2020-12-14 14:37

    I think Ramsay is wrong, react native have a fully support on base64 image. I found this

    https://facebook.github.io/react-native/docs/tabbarios.html
    

    this is a official example of how to create a iOS TabBarController, and they use a base64 image as one of the TabBar's icon.

    I think you did not specify the width and the height in the style property of the <Image/>.

    I tried to use your base64 image in my React Native Playground, and it works.

    Usage

    var base64Icon = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAMAAAAoLQ9TAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAwBQTFRF7c5J78kt+/Xm78lQ6stH5LI36bQh6rcf7sQp671G89ZZ8c9V8c5U9+u27MhJ/Pjv9txf8uCx57c937Ay5L1n58Nb67si8tVZ5sA68tJX/Pfr7dF58tBG9d5e8+Gc6chN6LM+7spN1pos6rYs6L8+47hE7cNG6bQc9uFj7sMn4rc17cMx3atG8duj+O7B686H7cAl7cEm7sRM26cq/vz5/v767NFY7tJM78Yq8s8y3agt9dte6sVD/vz15bY59Nlb8txY9+y86LpA5LxL67pE7L5H05Ai2Z4m58Vz89RI7dKr+/XY8Ms68dx/6sZE7sRCzIEN0YwZ67wi6rk27L4k9NZB4rAz7L0j5rM66bMb682a5sJG6LEm3asy3q0w3q026sqC8cxJ6bYd685U5a457cIn7MBJ8tZW7c1I7c5K7cQ18Msu/v3678tQ3aMq7tNe6chu6rgg79VN8tNH8c0w57Q83akq7dBb9Nld9d5g6cdC8dyb675F/v327NB6////AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA/LvB3QAAAMFJREFUeNpiqIcAbz0ogwFKm7GgCjgyZMihCLCkc0nkIAnIMVRw2UhDBGp5fcurGOyLfbhVtJwLdJkY8oscZCsFPBk5spiNaoTC4hnqk801Qi2zLQyD2NlcWWP5GepN5TOtSxg1QwrV01itpECG2kaLy3AYiCWxcRozQWyp9pNMDWePDI4QgVpbx5eo7a+mHFOqAxUQVeRhdrLjdFFQggqo5tqVeSS456UEQgWE4/RBboxyC4AKCEI9Wu9lUl8PEGAAV7NY4hyx8voAAAAASUVORK5CYII=';
    

    and use in this way

    <Image style={{width: 100, height: 50, resizeMode: Image.resizeMode.contain, borderWidth: 1, borderColor: 'red'}} source={{uri: base64Icon}}/>
    
    0 讨论(0)
  • 2020-12-14 14:42

    Yes it is supporting.

    Try below It is tested in iOS and below version "react": "16.9.0", "react-native": "0.61.2",

    const base64Image = '...';
    <Image source={{uri: `data:image/jpeg;base64,${base64Image}`}} />
    

    If still not working. check type of image i.e. jpeg, png etc.

    Hope this help :)

    0 讨论(0)
  • 2020-12-14 14:49
     <Image
         source={{
            uri: `data:image/jpeg;base64,${this.state.barcodeImage}`,
         }}
         style={{height: 200, width: 250}}
     />
    

    notice : add style in image

    0 讨论(0)
  • 2020-12-14 14:50

    I don't think this is supported. Unless the URL contains "http" or "https", it's considered a "static" image. Picking through the code, the instantiation of the final image boils down to:

    if ([path isAbsolutePath]) {
      image = [UIImage imageWithContentsOfFile:path];
    } else {
      image = [UIImage imageNamed:path];
      if (!image) {
        image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:path ofType:nil]];
      }
    }
    

    Step by step:

    1. If the URL is an absolute path, load it as a file
    2. Else if the image is cached (imageNamed), load that
    3. Else if the image is a relative path, load it from the app bundle

    Obviously none of that relates to base64. I've created a pull request for this feature which you can view here:

    https://github.com/facebook/react-native/pull/576

    0 讨论(0)
提交回复
热议问题