save-image-to-photos-album.uvue 1.4 KB
Newer Older
张磊 已提交
1
<template>
H
hdx 已提交
2 3 4
  <!-- #ifdef APP -->
  <scroll-view style="flex:1">
  <!-- #endif -->
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
5 6 7 8 9
    <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>
H
hdx 已提交
10 11 12
  <!-- #ifdef APP -->
  </scroll-view>
  <!-- #endif -->
张磊 已提交
13 14 15
</template>

<script>
H
hdx 已提交
16 17 18
  export default {
    data() {
      return {
19 20 21
        title: "saveImageToPhotosAlbum",
        // 自动化测试
        success: false
H
hdx 已提交
22 23 24 25 26 27
      }
    },
    methods: {
      saveImage() {
        uni.saveImageToPhotosAlbum({
          filePath: "/static/uni.png",
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
28 29
          success: (res) => {
            console.log("saveImageToPhotosAlbum success", JSON.stringify(res));
H
hdx 已提交
30 31 32 33
            uni.showToast({
              position: "center",
              icon: "none",
              title: "图片保存成功,请到手机相册查看"
34 35
            });
            this.success = true;
H
hdx 已提交
36
          },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
37
          fail: (err) => {
H
hdx 已提交
38
            uni.showModal({
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
39 40
              title: "保存图片到相册失败",
              content: JSON.stringify(err),
H
hdx 已提交
41 42
              showCancel: false
            });
43
            this.success = false;
H
hdx 已提交
44 45 46 47 48
          }
        })
      }
    }
  }
张磊 已提交
49 50 51
</script>

<style>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
52 53 54 55 56 57 58 59 60
  .margin-top-10 {
    margin-top: 10px;
  }

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