element-takesnapshot.uvue 2.8 KB
Newer Older
1 2 3 4 5
<template>
  <view>
    <page-head id="page-head" title="getElementById"></page-head>
    <view class="uni-padding-wrap" id="snapshot-content">
      <text id="text">this is text</text>
W
wanganxp 已提交
6
      <view id="view" class="uni-common-mt" style="border: 1px solid red">this is view</view>
7 8 9 10 11 12 13 14
      <button class="uni-btn" @click="changeViewStyle">
        修改 view 宽高及背景色
      </button>

      <button class="uni-btn" type="primary" @click="takeSnapshotClick">
        点击截图
      </button>
    </view>
W
wanganxp 已提交
15
    <image style="margin-top: 20px;" :src="snapImage" mode="aspectFit" @longpress="saveToAlbum"></image>
16 17 18 19
  </view>
</template>

<script lang="uts">
W
wanganxp 已提交
20 21 22 23 24 25 26
  export default {
    data() {
      return {
        snapImage: "/static/uni.png"
      }
    },
    methods: {
27

W
wanganxp 已提交
28 29 30
      changeViewStyle() {
        const text = uni.getElementById('text')!
        text.style.setProperty('color', 'red')
31

W
wanganxp 已提交
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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
        const view = uni.getElementById('view')!
        view.style.setProperty('width', '90%')
        view.style.setProperty('height', '50px')
        view.style.setProperty('backgroundColor', '#007AFF')
      },
      takeSnapshotClick() {
        const view = uni.getElementById('snapshot-content')!
        view.takeSnapshot({
          success: function (res : TakeSnapshotSuccess) {
            console.log(res.tempFilePath)
            this.snapImage = res.tempFilePath
            uni.showToast({
              title: '截图成功,路径:' + res.tempFilePath,
              icon: "none"
            })
          },
          fail: function (res : TakeSnapshotFail) {
            console.log(res)
            uni.showToast({
              icon: 'error',
              title: '截图失败'
            })
          }
        } as TakeSnapshotOptions)
      },
      saveToAlbum(e : TouchEvent) {
        // console.log(e.currentTarget!.getAttribute("src"));
        let filePath : string = e.currentTarget!.getAttribute("src") as string
        uni.showActionSheet({
          itemList: ["保存"],
          success: res => {
            // console.log(res.tapIndex);
            if (res.tapIndex == 0) {
              uni.saveImageToPhotosAlbum({
                filePath: filePath,
                success() {
                  uni.showToast({
                    position: "center",
                    icon: "none",
                    title: "图片保存成功,请到手机相册查看"
                  })
                },
                fail(e) {
                  uni.showModal({
                    content: "保存相册失败,errCode:" + e.errCode + ",errMsg:" + e.errMsg + ",errSubject:" + e.errSubject,
                    showCancel: false
                  });
                }
              })
            }
          },
          fail: () => { },
          complete: () => { }
        });
      }
87 88 89
    }
  }
</script>