uni-app 點擊預覽圖片長按保存圖片到本地


存檔備用 =A=

<template>
  <view>
    <view>
      <image
        class="img-style"
        :src="src"
        alt="demoImg"
        @click="previewImg()"
      ></image>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      src: "/static/logo.png",
    };
  },
  methods: {
    previewImg() {
      uni.previewImage({
        urls: [this.src], //需要預覽的圖片http鏈接列表,多張的時候,url直接寫在后面就行了
        current: "", // 當前顯示圖片的http鏈接,默認是第一個
        success: function (res) {},
        fail: function (res) {},
        complete: function (res) {},
	//長按保存到本地
        longPressActions: {
          itemList: ["保存圖片到本地"],
          success: (data) => {
            if (data.tapIndex == 0) {
              let url = this.src;
              uni.saveImageToPhotosAlbum({
                filePath: url,
                success: (res) => {
                  uni.showToast({
                    title: "已存至系統相冊",
                    icon: "success",
                  });
                },
                fail: (res) => {
                  uni.showToast({
                    title: "保存失敗",
                    icon: "error",
                  });
                },
              });
            }
          },
          fail: function (err) {
            console.log(err.errMsg);
          },
        },
      });
    },
  },
};
</script>

<style lang="less" scoped>
.img-style {
  margin: 32rpx 0;
  height: 450rpx;
  width: 660rpx;
}
</style>

參考文檔:https://uniapp.dcloud.io/api/media/image?id=unipreviewimageobject


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM