我在用react-native-image-picker上传照片。这似乎对从相机胶卷中拉出的照片很有效,但如果用户选择拍摄照片,则响应中没有数据,只有空字符串。
我使用以下选项:
{
title: 'Upload Photo',
cancelButtonTitle: 'Cancel',
takePhotoButtonTitle: 'Take Photo...',
chooseFromLibraryButtonTitle: 'Choose from Library...',
noData: false,
mediaType: 'photo',
quality: 0.2,
}并使用以下命令启动图像选取器:
UIImagePickerManager.showImagePicker(options, (response) => {
console.log(response);
});可以很好地处理从相机胶卷中取出的图像,但当使用相机拍摄新图像时,将为response.data提供一个空字符串。
怎么回事?
发布于 2020-02-19 16:12:11
如果您想使用data (noData:false)显示图像,请使用以下命令:
UIImagePickerManager.showImagePicker(options, response => {
if (response.didCancel) {
console.log('User cancelled image picker');
} else if (response.error) {
console.log('ImagePicker Error: ', response.error);
} else if (response.customButton) {
console.log('User tapped custom button: ', response.customButton);
} else {
const source = {uri: 'data:image/jpeg;base64,' + response.data};
this.setState({
avatarSource: source,
});
}
});并为图像源设置avatarSource状态
https://stackoverflow.com/questions/35855805
复制相似问题