image-format.uvue 3.1 KB
Newer Older
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
1
<template>
xuty73419315's avatar
xuty73419315 已提交
2
  <scroll-view style="flex: 1;">
雪洛's avatar
雪洛 已提交
3 4
    <page-head :title="title"></page-head>
    <view class="uni-padding-wrap uni-common-mt">
W
wanganxp 已提交
5
      <!-- <view class="uni-title">
雪洛's avatar
雪洛 已提交
6
        <text class="uni-title-text">支持的图片格式示例</text>
W
wanganxp 已提交
7
      </view> -->
雪洛's avatar
雪洛 已提交
8 9 10
      <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 已提交
11 12 13
          <!-- <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
雪洛 已提交
14 15
        </view>
      </view>
W
wanganxp 已提交
16
      <!-- <view class="uni-title">
雪洛's avatar
雪洛 已提交
17 18 19 20 21 22 23 24
        <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 已提交
25
      </view> -->
雪洛's avatar
雪洛 已提交
26 27
    </view>
  </scroll-view>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
28 29 30
</template>

<script>
雪洛's avatar
雪洛 已提交
31 32 33 34 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
  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 已提交
61
            format: 'heic(App-Android10+支持)',
雪洛's avatar
雪洛 已提交
62
            src: '/static/test-image/logo.heic'
W
wanganxp 已提交
63 64 65 66 67 68 69 70
          },
          {
            format: 'avif(仅部分浏览器支持)',
            src: '/static/test-image/logo.avif'
          },
          {
            format: 'tif(仅部分浏览器支持)',
            src: '/static/test-image/logo.tif'
雪洛's avatar
雪洛 已提交
71
          }
W
wanganxp 已提交
72 73
        ] as Array<ImageFormat>
        /* notSupportFormats: [
雪洛's avatar
雪洛 已提交
74 75 76 77 78 79 80 81
          {
            format: 'avif',
            src: '/static/test-image/logo.avif'
          },
          {
            format: 'tif',
            src: '/static/test-image/logo.tif'
          }
W
wanganxp 已提交
82
        ] as Array<ImageFormat> */
雪洛's avatar
雪洛 已提交
83 84 85 86
      }
    },
    methods: {
      imageErrorEvent(index: number, e: ImageErrorEvent) {
W
wanganxp 已提交
87
        console.log("图片加载错误", e.detail);
雪洛's avatar
雪洛 已提交
88
        // 图片加载失败,加载本地占位图
W
wanganxp 已提交
89
        this.supportFormats[index].errorImage = '/static/template/drop-card/dislike.png'
雪洛's avatar
雪洛 已提交
90 91 92 93 94 95 96 97 98
      }
    },
  }

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

<style>
雪洛's avatar
雪洛 已提交
102
  .image {
W
wanganxp 已提交
103 104
    margin: 40px auto;
    width: 100px;
雪洛's avatar
雪洛 已提交
105 106
  }
</style>