我正在使用react-native-image-picker和react-native-photo-editor ..
照片編輯器回傳已編輯影像的 uri,而我的 api 支持 base64,有沒有辦法將編輯后的影像 uri 轉換為 base64 編碼?
這是我下面的代碼
const handleOnUploadFromLibrary = () => {
launchImageLibrary(libraryOptions, (response: ImagePickerResponse) => {
if (response.assets) {
let assets = response.assets[0];
let imagePathMinusExtention = assets.uri?.replace('file://', '');
setImagePickerAsset({...assets, uri: imagePathMinusExtention});
}
PhotoEditor.Edit({
path: imagePickerAsset?.uri || '',
onDone: (path) => {
console.log('donee::::');
let extention = "file://";
let imagePathPlusExtention = extention.concat(`${path}`);
setImagePickerAsset({...imagePickerAsset, uri: imagePathPlusExtention});
var data: UsedAttachment = {
fileType: imagePickerAsset?.type,
fileName: imagePickerAsset?.fileName,
refId: visit.draftAppRequestId,
refTypeId: '5', //draftx
title: imagePickerAsset?.fileName,
mimeType: imagePickerAsset?.type,
modelMapperKey: '',
files: [imagePickerAsset?.uri],
};
callUploadAttachment({data});
hideDialog();
},
onCancel: () => {
console.log('Cancel ::: ');
hideDialog();
},
});
});
};
uj5u.com熱心網友回復:
如果您使用的是 react-native cli,則可以使用react-native-fs
檔案:https : //github.com/itinance/react-native-fs#readfilefilepath-string-encoding-string-promisestring
import RNFS from 'react-native-fs';
//base64 res
var data = await RNFS.readFile( "file://path-to-file", 'base64').then(res => { return res });
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/365453.html
