save-image-to-photos-album.uvue 1.5 KB
Newer Older
DCloud-WZF's avatar
DCloud-WZF 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 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
<template>
  <!-- #ifdef APP -->
  <scroll-view style="flex:1">
  <!-- #endif -->
    <page-head :title="title"></page-head>
    <view class="uni-padding-wrap">
      <image class="image" src="/static/uni.png"></image>
      <button class="margin-top-10" type="primary" @click="saveImage">将图片保存到手机相册</button>
    </view>
  <!-- #ifdef APP -->
  </scroll-view>
  <!-- #endif -->
</template>

<script>
  export default {
    data() {
      return {
        title: "saveImageToPhotosAlbum",
        // 自动化测试
        success: false
      }
    },
    methods: {
      saveImage() {
        uni.saveImageToPhotosAlbum({
          filePath: "/static/uni.png",
          success: (res) => {
            console.log("saveImageToPhotosAlbum success", JSON.stringify(res));
            uni.showToast({
              position: "center",
              icon: "none",
              title: "图片保存成功,请到手机相册查看"
            });
            this.success = true;
          },
          fail: (err) => {
            uni.showModal({
              title: "保存图片到相册失败",
              content: JSON.stringify(err),
              showCancel: false
            });
            this.success = false;
          }
        })
      }
    }
  }
</script>

<style>
  .margin-top-10 {
    margin-top: 10px;
  }

  .image {
    width: 196px;
    height: 196px;
    align-self: center;
  }
DCloud-yyl's avatar
DCloud-yyl 已提交
61
</style>