首页 前端知识 vue h5 保存图片到手机本地相册的方法

vue h5 保存图片到手机本地相册的方法

2024-06-24 23:06:58 前端知识 前端哥 731 699 我要收藏

效果图
在这里插入图片描述
1、插件和上传接口

import html2canvas from 'html2canvas'
import { upload } from '@/api/accept'

2、结构

<button @click='generateImage'>生成</button>

<!-- 中间信息展示 -->
      <div class="sevenInfo textLeft" id="target-element">
        <div class="name">{{ info ? info.auserName : '天翼爱音乐科技有限公司' }}</div>
        
        <div class="congratulation">
          <img src="@/assets/images/annualReport/congratulation.png" alt="" />
          <span>{{month.currentDate}}</span>
          <img class="mon" :src="imageSource" alt="" />
        </div>
        <div class="number">
          <div class="title">{{month.currentDate}}月商务视频彩铃播放成绩:</div>

          <div v-if="!tag" class="items">
            <div class="item">
              <span>观看人数{{ YearData6.watch_uv }}</span>
            </div>
            <div class="item">
              <span>人员遍布城市{{ YearData6.city }}</span>
            </div>
            <div class="item">
              <span>播放次数{{ YearData6.all_pv }}</span>
            </div>
            <div class="item">
              <span>播放时长{{ YearData6.total_duration }}</span>
            </div>
          </div>
          <div v-else class="items">
            <div class="item">
              <span>观看人数1716</span>
            </div>
            <div class="item">
              <span>人员遍布城市229</span>
            </div>
            <div class="item">
              <span>播放次数2297</span>
            </div>
            <div class="item">
              <span>播放时长29,443</span>
            </div>
          </div>
        </div>
        <div class="comeItem">
          <span>{{month.currentDate+1}}</span>
          <img class="come" src="@/assets/images/annualReport/come.png" alt="" />
        </div>
      </div>

  generateImage() {
      this.$toast('生成海报后长按分享')
      let that = this
      setTimeout(function () {
        console.log("执行");
        
        const element = document.getElementById('target-element') // 替换为目标元素的id或class
        html2canvas(element,{backgroundColor: '#0e0e0e'}).then(canvas => {

          const image = canvas.toDataURL('image/png')
          scale: 5
          dpi: 600, console.log('image', image)
          let url = that.dataURLtoBlob(image)
          console.log('url', url)
          that.videoFile(url)
        })
      }, 2000) 
    },

// 将base64转成blob 为了后续能上传
    dataURLtoBlob(dataurl) {
      var arr = dataurl.split(',')
      //注意base64的最后面中括号和引号是不转译的
      var _arr = arr[1].substring(0, arr[1].length - 2)
      var mime = arr[0].match(/:(.*?);/)[1],
        bstr = atob(_arr),
        n = bstr.length,
        u8arr = new Uint8Array(n)
      while (n--) {
        u8arr[n] = bstr.charCodeAt(n)
      }
      return new Blob([u8arr], {
        type: mime
      })
    },
// 上传图片
    videoFile(file) {
      let params = new window.FormData()
      let fileOfBlob = new File([file], new Date().getTime() + '.jpg')
      params.append('file', fileOfBlob)

      upload(params).then(res => {
        console.log('上传结果', res)
        if (res.success) {
          this.url = res.data.url
          this.savePic(res.data.url)
        }
      })
    },
转载请注明出处或者链接地址:https://www.qianduange.cn//article/13502.html
标签
评论
发布的文章
大家推荐的文章
会员中心 联系我 留言建议 回顶部
复制成功!