在Javascript中将本地图像转换为base64字符串。我正在android设备和图像路径中进行测试,例如:
../../ Images / Stickers / Bigottario / 14.png
如何将图像从项目本地文件夹转换为base64? 我已经尝试使用下面给出的react-native-fs。
RNFS.readFile(this.state.StateStrickersobject.imagePath, 'base64').then(image => {
alert("22222222222222222"+image)
});
答案 0 :(得分:-1)
示例图像上传器组件可能对您有所帮助。 注意:需要 react-native-image-picker 模块。翻译服务是可选的
import React from 'react';
import { View, TouchableOpacity, ScrollView, Alert } from 'react-native';
import {
Item, Label, Thumbnail, Button, Icon, Text
} from 'native-base';
import ImagePicker from 'react-native-image-picker';
import { translate } from '../../Services/Translation/TranslationService';
selectPhotoTapped = ({ Image, onDataChanged }) => {
const options = { quality: 1.0, maxWidth: 500, maxHeight: 500, storageOptions: { skipBackup: true } };
ImagePicker.showImagePicker(options, (response) => {
if (response.didCancel) { // console.log('User cancelled photo picker');
}
else if (response.error) { // console.log('ImagePicker Error: ', response.error);
}
else if (response.customButton) { // console.log('User tapped custom button: ', response.customButton);
}
else {
Image = Image ? Image : {};
Image.uri = response.uri;
Image.ImageStream = response.data;
onDataChanged(Image);
}
});
}
const ImageUploader = (props) => {
return (
<View>
<Item fixedLabel style={{ paddingRight: 10 }} error={props.error}>
<Label>
{props.children}
</Label>
<Button block onPress={() => selectPhotoTapped(props)} style={{ backgroundColor: "#8995A6" }}>
<Text uppercase={false}>{translate("choose_file")}</Text>
<Icon name='camera' />
</Button>
</Item>
</View>
);
}
export { ImageUploader };
示例显示方法
<ImageUploader
error={checkError(`${prefix}DocumentImages`)}
Image={this.state.image}
onDataChanged={(image) => console.log(image)} >
<Label> Select photo</Label>
</ImageUploader>