React Native save base64 image to Album

前端 未结 5 1771
没有蜡笔的小新
没有蜡笔的小新 2021-02-05 21:22

Third Party API return a \"QR code image\" in base64 encode,
I need save that image to User\'s Album.

  • CamerRoll - not support saving base64 image to album <
5条回答
  •  日久生厌
    2021-02-05 21:30

    I solve the problem,
    turn out I forgot data:image/png;base64, at beginning of the string.

    I remove it with following code

      // json.qr is base64 string
      var image_data = json.qr.split('data:image/png;base64,');
      image_data = image_data[1];
    

    and then save the image file

      import fetch_blob from 'react-native-fetch-blob';
      import RNFS from 'react-native-fs';
    
      const fs = fetch_blob.fs
      const dirs = fetch_blob.fs.dirs      
      const file_path = dirs.DCIMDir + "/bigjpg.png"
    
      // json.qr is base64 string "data:image/png;base64,..."
    
      var image_data = json.qr.split('data:image/png;base64,');
      image_data = image_data[1];
    
      RNFS.writeFile(file_path, image_data, 'base64')
      .catch((error) => {
        alert(JSON.stringify(error));
      });
    

    I wrote a blog about this

    http://1c7.me/react-native-save-base64-image-to-album/

提交回复
热议问题