|
@@ -15,6 +15,7 @@
|
|
|
mode="aspectFill"
|
|
|
class="img-item"
|
|
|
@click="preImg(imgList, index)"
|
|
|
+ @error="errorImage(item, index)"
|
|
|
/>
|
|
|
</view>
|
|
|
</div>
|
|
@@ -23,7 +24,8 @@
|
|
|
</template>
|
|
|
|
|
|
<script setup lang="ts">
|
|
|
-import { onMounted, ref } from 'vue'
|
|
|
+import { onMounted, ref, getCurrentInstance } from 'vue'
|
|
|
+import heic2any from 'heic2any'
|
|
|
|
|
|
const props = defineProps({
|
|
|
item: Object,
|
|
@@ -64,6 +66,54 @@ const preImg = (imgList: any[], index: number) => {
|
|
|
urls: arr
|
|
|
})
|
|
|
}
|
|
|
+const errorImage = async (item: any, index: number) => {
|
|
|
+ // #ifdef H5
|
|
|
+ await handleImage(item, index)
|
|
|
+
|
|
|
+ // #endif
|
|
|
+}
|
|
|
+const instance = getCurrentInstance()
|
|
|
+
|
|
|
+const imgValue = ref('')
|
|
|
+const handleImage = async (item: any, index: number) => {
|
|
|
+ let url = item.path
|
|
|
+ const response = await fetch(url)
|
|
|
+ const blob = await response.blob()
|
|
|
+
|
|
|
+ if (await isHeicFile(blob)) {
|
|
|
+ const heicImageBlob = await fetch(url).then((res) => res.blob())
|
|
|
+
|
|
|
+ // 使用 heic2any 库将 HEIC 转换为 JPG
|
|
|
+ heic2any({ blob: heicImageBlob, toType: 'image/jpeg' })
|
|
|
+ .then((convertedBlob) => {
|
|
|
+ // @ts-ignore
|
|
|
+ const imgURL = URL.createObjectURL(convertedBlob)
|
|
|
+ imgList.value[index].path = imgURL
|
|
|
+ imgValue.value = imgURL
|
|
|
+ instance?.proxy?.$forceUpdate()
|
|
|
+ })
|
|
|
+ .catch((err) => {
|
|
|
+ console.log('err', err)
|
|
|
+ })
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+const isHeicFile = (file: any) => {
|
|
|
+ return new Promise((resolve, reject) => {
|
|
|
+ const reader = new FileReader()
|
|
|
+ reader.onloadend = () => {
|
|
|
+ const buffer: string | ArrayBuffer | null = reader.result
|
|
|
+ // HEIC 文件的前几个字节为 "00 00 00 18 66 74 79 70 68 65 69 63" 或 "00 00 00 20 66 74 79 70 68 65 69 63" (小端字节序)
|
|
|
+ const signature = new Uint8Array(buffer as ArrayBuffer).subarray(0, 12)
|
|
|
+ const heicSignature = [0x00, 0x00, 0x00, 0x18, 0x66, 0x74, 0x79, 0x70, 0x68, 0x65, 0x69, 0x63]
|
|
|
+
|
|
|
+ const isHeic = signature.every((byte, index) => byte === heicSignature[index])
|
|
|
+ resolve(isHeic)
|
|
|
+ }
|
|
|
+ reader.onerror = reject
|
|
|
+ reader.readAsArrayBuffer(file)
|
|
|
+ })
|
|
|
+}
|
|
|
</script>
|
|
|
|
|
|
<style lang="scss" scoped>
|