image-format.uvue 3.2 KB
Newer Older
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
1
<template>
2
  <!-- #ifdef APP -->
xuty73419315's avatar
xuty73419315 已提交
3
  <scroll-view style="flex: 1;">
4
  <!-- #endif -->
雪洛's avatar
雪洛 已提交
5 6
    <page-head :title="title"></page-head>
    <view class="uni-padding-wrap uni-common-mt">
W
wanganxp 已提交
7
      <!-- <view class="uni-title">
雪洛's avatar
雪洛 已提交
8
        <text class="uni-title-text">支持的图片格式示例</text>
W
wanganxp 已提交
9
      </view> -->
雪洛's avatar
雪洛 已提交
10 11 12
      <view v-for="(item,index) in supportFormats" :key="index">
        <text class="uni-subtitle-text">{{item.format}}</text>
        <view class="uni-center" style="background:#FFFFFF;">
W
wanganxp 已提交
13 14 15
          <!-- <image class="image" mode="widthFix" :src="item.src"></image> -->
          <image class="image" mode="widthFix" :src="item.errorImage == null ? item.src : item.errorImage"
            @error="imageErrorEvent(index, $event as ImageErrorEvent)"></image>
雪洛's avatar
雪洛 已提交
16 17
        </view>
      </view>
W
wanganxp 已提交
18
      <!-- <view class="uni-title">
雪洛's avatar
雪洛 已提交
19 20 21 22 23 24 25 26
        <text class="uni-title-text">暂不支持的格式</text>
      </view>
      <view v-for="(item,index) in notSupportFormats" :key="index">
        <text class="uni-subtitle-text">{{item.format}}</text>
        <view class="uni-center" style="background:#FFFFFF;">
          <image class="image" mode="widthFix" :src="item.errorImage == null ? item.src : item.errorImage"
            @error="imageErrorEvent(index, $event as ImageErrorEvent)"></image>
        </view>
W
wanganxp 已提交
27
      </view> -->
雪洛's avatar
雪洛 已提交
28
    </view>
29
  <!-- #ifdef APP -->
雪洛's avatar
雪洛 已提交
30
  </scroll-view>
31
  <!-- #endif -->
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
32 33 34
</template>

<script>
雪洛's avatar
雪洛 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
  export default {
    data() {
      return {
        title: 'image-format',
        supportFormats: [
          {
            format: 'bmp',
            src: '/static/test-image/logo.bmp'
          },
          {
            format: 'gif',
            src: '/static/test-image/logo.gif'
          },
          {
            format: 'ico',
            src: '/static/test-image/logo.ico'
          },
          {
            format: 'jpg',
            src: '/static/test-image/logo.jpg'
          },
          {
            format: 'png',
            src: '/static/test-image/logo.png'
          },
          {
            format: 'webp',
            src: '/static/test-image/logo.webp'
          },
          {
W
wanganxp 已提交
65
            format: 'heic(App-Android10+支持)',
雪洛's avatar
雪洛 已提交
66
            src: '/static/test-image/logo.heic'
W
wanganxp 已提交
67 68 69 70 71 72 73 74
          },
          {
            format: 'avif(仅部分浏览器支持)',
            src: '/static/test-image/logo.avif'
          },
          {
            format: 'tif(仅部分浏览器支持)',
            src: '/static/test-image/logo.tif'
雪洛's avatar
雪洛 已提交
75
          }
W
wanganxp 已提交
76 77
        ] as Array<ImageFormat>
        /* notSupportFormats: [
雪洛's avatar
雪洛 已提交
78 79 80 81 82 83 84 85
          {
            format: 'avif',
            src: '/static/test-image/logo.avif'
          },
          {
            format: 'tif',
            src: '/static/test-image/logo.tif'
          }
W
wanganxp 已提交
86
        ] as Array<ImageFormat> */
雪洛's avatar
雪洛 已提交
87 88 89 90
      }
    },
    methods: {
      imageErrorEvent(index: number, e: ImageErrorEvent) {
W
wanganxp 已提交
91
        console.log("图片加载错误", e.detail);
雪洛's avatar
雪洛 已提交
92
        // 图片加载失败,加载本地占位图
W
wanganxp 已提交
93
        this.supportFormats[index].errorImage = '/static/template/drop-card/dislike.png'
雪洛's avatar
雪洛 已提交
94 95 96 97 98 99 100 101 102
      }
    },
  }

  type ImageFormat = {
    format : string
    src : string
    errorImage ?: string | null
  }
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
103 104 105
</script>

<style>
雪洛's avatar
雪洛 已提交
106
  .image {
W
wanganxp 已提交
107 108
    margin: 40px auto;
    width: 100px;
雪洛's avatar
雪洛 已提交
109 110
  }
</style>