react-native-view-shot:Base64字符串轉換成本地圖片

場景:
使用react-native-view-shot,截屏獲得N個base64的字符串,在不修改原邏輯的情況下,需要把其中一個截屏保留成本地圖片,即把base64字符串寫成本地圖片。
代碼如下:

 _saveImageFromBase64Str = base64Str => {
 //因爲截取的使用選擇了'data-uri'的模式截取,所以字符串中含有"data:image/png;base64,"
 //僅需逗號後面的字符串
    let firstIdex = base64Str.indexOf(',') + 1;
    let imageStr = base64Str.substring(firstIdex);
    let filename ='xxxxxx.png';

    RNFS.writeFile(filename, imageStr, 'base64')
      .then(res => {})
      .catch(err => {});
  };
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章