場景:
使用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 => {});
};