uniapp开发微信小程序保存图片或视频到本地相册(手指放大)

点击图片或视频进入的预览页面

数据结构,例:
list :[{
	url: '/image/plus.png',
	type: 1,
}, {
	url: '/video/plus.mp4',
	type: 2,
}]
<template>
  <view class="w-full h-screen flex flex-col bg-black relative">
    <swiper class="w-full h-screen" :current="current" @change="change">
      <swiper-item v-for="(item, index) in list" :key="index">
        <movable-area v-if="Number(item.type) === 1" scale-area class="w-full h-full">
          <movable-view class="w-full h-full" direction="all" scale="true" scale-min="1" scale-max="4" :scale-value="scale" @dblclick="dblclick">
            <image v-if="item.url" :src="imgJoin(item.url)" class="w-full h-full" mode="aspectFit" show-menu-by-longpress @click="back" />
            <image v-if="item.content" :src="imgJoin(item.content)" class="w-full h-full" mode="aspectFit" show-menu-by-longpress @click="back" />
          </movable-view>
        </movable-area>
        <template v-if="Number(item.type) === 2">
          <video v-if="item.content" :src="imgJoin(item.content)" class="w-full h-full"></video>
          <video v-if="item.url" :src="imgJoin(item.url)" class="w-full h-full"></video>
        </template>
      </swiper-item>
    </swiper>
    <view class="text-white fixed bottom-80 left-0 px-4 py-2 box-border w-full flex justify-between items-center text-base">
      <view class="">{{ Number(current) + 1 }}/{{ list.length || 0 }}</view>
      <view v-if="list[current]" @click.stop="onStartDownload">{{ Number(list[current].type) === 1 ? '长按图片保存' : '点击保存' }}</view>
    </view>
  </view>
</template>

<script>
export default {
  components: {},
  props: {
  },
  data() {
    return {
      scale: 1, // 放大缩小倍数
      current: 1, // swiper当前index
      list: [],
    }
  },
  computed: {},
  watch: {},
  onLoad(options) {
    console.log('options', options)
    this.current = options ? options.index : 0
    this.list = JSON.parse(JSON.stringify(this.$store.state.previewList))
    uni.hideHomeButton()
  },
  onShow() {},
  methods: {
    change(e) {
      this.current = e.detail.current
    },
    dblclick() {
      if (this.scale === 10) {
        this.scale = 1
      } else {
        this.scale = 10
      }
    },
    back() {
      uni.navigateBack()
    },
    // 下载视频到本地
    downVideo() {
      const data = this.list[this.current]
      if (Number(data.type) !== 2) return
      uni.showLoading()
      uni.downloadFile({
        url: this.imgJoin(data.url),
        timeout: 30000,
        header: {
          "Content-Type": "video/mp4"
        },
        success: res => {
          uni.saveVideoToPhotosAlbum({
            filePath: res.tempFilePath,
            success: res => {
              uni.hideLoading()
              uni.showToast({
                title: "保存成功",
                duration: 2000,
              })
            }
          })
        },
        fail() {
          uni.hideLoading()
        }
      })
      uni.hideLoading()
    },
    onStartDownload() {
      wx.getSetting({
        success: (res) => {
          if (res.authSetting['scope.writePhotosAlbum'] === undefined) {
            wx.authorize({
              scope: 'scope.writePhotosAlbum',
              success: () => {
                this.downVideo()
              },
              fail: (err) => {
                console.log('授权失败:', err)
              }
            })
          } else if (!res.authSetting['scope.writePhotosAlbum']) {
            wx.openSetting({
              success: (res) => {
                if (res.authSetting['scope.writePhotosAlbum']) {
                  this.downVideo()
                }
              }
            })
          } else {
            this.downVideo()
          }
        }
      })
    },
    save() {
      // #ifdef MP-WEIXIN
      // uni.chooseImage({
      //   count: 1,
      //   sourceType: ['camera'],
      //   success: function(res) {
      //     uni.saveImageToPhotosAlbum({
      //       filePath: this.imgJoin(this.list[this.current - 1].url),
      //       success: res => {
      //         uni.showToast({
      //           title: '保存成功',
      //           icon: 'success',
      //           duration: 2000
      //         })
      //       },
      //       fail: err => {
      //         uni.showToast({
      //           title: '保存失败',
      //           icon: 'none',
      //           duration: 2000
      //         })
      //       }
      //     })
      //   },
      // })
      // #endif
    },
  },
}
</script>

<style lang="scss" scoped></style>

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值