1.安裝HEIC轉(zhuǎn)化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'))) {
? ? ? ? ? ? //根據(jù)origURL轉(zhuǎn)換成JPG圖片: 這有個(gè)雷區(qū)排拷,剛開(kāi)始使用的response.uri.在IOS中uri的heic文件后綴也是JPG盲链,所以不能使用颗管。查看API后做盅,應(yīng)該使用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類(lèi)型轉(zhuǎn)jpg失敗!"});
????????????????}
????????????});
? ? ? ? } else {
????????????????source = {uri: response.uri, type: response.type, name: response.fileName}
????????????????this.handleChange(source) this.setState({ loading: false });
????????}
}
});