我正在从画廊中挑选照片并上传到服务器但是几天后我注意到有些照片有扩展名,浏览器无法呈现这些图片。 1.有没有办法从上传的heic中提取照片? 2.如何反应原生我可以从这种格式获得jpeg?
你可以在服务器端转换它,借助这个很棒的lib Tifig。
https://github.com/monostream/tifig
您也可以使用像https://cloudconvert.com/formats/image/heic这样的API来转换服务器端或移动设备(客户端)。
我假设您使用的是react-native-image-picker,这是社区库,也是最常用的库。
实际上没有必要安装另一个模块,只需从响应中抓取uri
并更新文件名,以防你有一个heic图像。代码示例:
const options = {
title: 'Select Avatar',
storageOptions: {
skipBackup: true,
path: 'images'
},
noData: true,
quality: 1,
mediaType: 'photo'
};
ImagePicker.showImagePicker(options, imgResponse => {
this.setState({ imageLoading: true, avatarMediaId: null });
if ((imgResponse.didCancel) || (imgResponse.error)) {
this.setState({ imageLoading: false });
} else {
let source = {};
let fileName = imgResponse.fileName;
if (Platform.OS === 'ios' && (fileName.endsWith('.heic') || fileName.endsWith('.HEIC'))) {
fileName = `${fileName.split(".")[0]}.JPG`;
}
source = { uri: imgResponse.uri, fileName };
this.uploadImage(source);
}
});