使用 ionic + cordova + vue3 实现相册选择、拍照,并上传、预览图片

本文详细描述了如何在基于ionic和vue的项目中实现上传组件upload.vue,包括模板规划、拍照和相册选择、文件上传、图片类型校验,以及使用Cordova插件处理拍照、相册选择的常见问题。同时介绍了图片放大组件enlarge-image.vue的实现和swiper的使用。

目录

1.上传组件 upload.vue

1.1 模板规划

1.2 点击添加按钮

1.2.1 实现询问弹框

1.2.2 实现拍照 

1.2.3 实现相册选择

 1.2.4 实现文件上传

1.2.5 校验图片类型并上传

1.2.6 获取图片列表

1.2.7 在组件内 添加图片附件

2.图片放大组件 enlarge-image.vue

2.1 点击图片放大

2.2 模板规划

2.3 使用 swiper11 踩坑的过程


1.上传组件 upload.vue

1.1 模板规划

模板包含三部分:

  • 已经上传的图片列表展示,若只读,则不展示删除按钮,每个图片点击后都可以被放大
  • 添加按钮展示,若没有图片,并且非只读,则展示
  • 暂无图片提示
  <div class="t-upload">
    <ion-grid>
      <!-- {
  
  { fileList }} -->

      <!-- 已经上传的图片 -->
      <template v-if="fileList?.length">
        <ion-col v-for="(img, index) in fileList" :key="img?.FILE_ID" size="4">
          <img
            class="file"
            :src="getImgUrl(img)"
            alt=""
            @click="goEnlargeImage(index)"
          />
          <img
            v-if="!readonly"
            class="delete"
            src="@/assets/image/common/upload-delete.png"
            @click.stop="removeFile(img?.FILE_ID)"
          />
        </ion-col>
      </template>

      <!-- 添加图片按钮 -->
      <template v-if="!readonly">
        <ion-col v-if="!fileList?.length || fileList?.length < 9" size="4">
          <img
            class="add-file"
            src="@/assets/image/common/upload-add.png"
            @click="addMediaFile()"
          />
        </ion-col>
      </template>

      <template v-if="!fileList?.length && readonly">
        <div class="fs-14">暂无附件</div>
      </template>
    </ion-grid>
  </div>

1.2 点击添加按钮

点击添加按钮后,会出现一个弹框,让用户选择图片来源:

  • 拍照
  • 相册选择

1.2.1 实现询问弹框

这个很简单,使用 ionic 的 actionSheetController 即可实现,根据用户的选择,决定执行的方法

  async addFile(max: number, callback: any) {
    const actionSheet = await actionSheetController.create({
      header: '附件类型选择',
      buttons: [
        {
          text: '拍照',
          handler: () => {
            this.camera({
              quality: 100,
              destinationType: 1,
              sourceType: 1,
              targetWidth: 1080,
              targetHeight: 1920,
              mediaType: 0,
              encodingType: 1,
            })
              .then(async (res) => {
                callback(res, 'photo');
              })
              .catch((err) => {
                publicService.toast('拍照失败,请重试');
              });
          },
        },
        {
          text: '相册',
          handler: () => {
            this.slectImagePicker({
              maximumImagesCount: max,
              quality: 50,
            })
              .then((res) => {
                callback(res, 'img');
              })
              .catch(() => {
                publicService.toast('相册打开失败');
              });
          },
        },
        {
          text: '取消',
          role: 'cancel',
          handler: () => {
            console.error('Cancel clicked');
          },
        },
      ],
    });
    await actionSheet.present();
  }

1.2.2 实现拍照 

安装 cordova 插件:

  • @awesome-cordova-plugins/camera@6.4.0
  • co
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Lyrelion

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值