1.安装HEIC转化JPG:react-native-heic-converter
https://github.com/maxim-kolesnikov/react-native-heic-converter
npm install react-native-heic-converter --save
react-native link react-native-heic-converter
2.源代码
import ImagePicker from 'react-native-image-picker';
import RNHeicConverter from 'react-native-heic-converter';
ImagePicker.launchImageLibrary(options, (response) => {
if (response.didCancel) {
console.log('User cancelled image picker');
} else if (response.error) {
console.log('ImagePicker Error: ', response.error);
} else {
let source = {}
//判断是不是HEIC文件
if (Platform.OS === 'ios' && (response.fileName.endsWith('.heic') || response.fileName.endsWith('.HEIC'))) {
//根据origURL转换成JPG图片: 这有个雷区,刚开始使用的response.uri.在IOS中uri的heic文件后缀也是JPG,所以不能使用。查看API后,应该使用origURL。(origURLT:he URL of the original asset in photo library, if it exists)
RNHeicConverter.convert({ path: response.origURL }).then((data) => {
const { success, path, error } = data
if(!error && success && path) {
let name = response.fileName
name = name.replace(".heic", ".jpg")
name = name.replace(".HEIC", ".jpg")
source = {uri: path, name}
this.handleChange(source)
this.setState({ loading: false });
} else {
Toast.show({text: "heic类型转jpg失败!"});
}
});
} else {
source = {uri: response.uri, type: response.type, name: response.fileName}
this.handleChange(source) this.setState({ loading: false });
}
}
});
网友评论