H5+ 保存圖片到手機相冊


box11 為保存內容的id
saveImg() {
      this.$toast.loading({
        message: "保存中...",
        forbidClick: true,
      });
      this.createPoster();
    },
    createPoster() {
      const poster = document.getElementById("box11");
      html2canvas(poster, {
        dpi: window.devicePixelRatio * 2,
        backgroundColor: "#fff",
        useCORS: true,
        scale: 2,
        async: true,
      }).then((canvas) => {
        this.dataImg = canvas.toDataURL("image/png");
        console.log(this.dataImg);
        this.toImage();
      });
    },
    toImage() {
      let filename = "下載二維碼" + ".png";
      console.log(window.plus);
      if (!window.plus) {
        this.$toast("請長按圖片保存到本地");
        return;
      }

      console.log("保存");
      let bitmap = new plus.nativeObj.Bitmap();
      bitmap.loadBase64Data(this.dataImg);
      bitmap.save(
        "_doc/" + filename,
        {
          check: true, // 設置為檢測白屏
          overwrite: true,
          quality: 100,
        },
        function(i) {
          //保存到系統相冊
          plus.gallery.save(
            i.target,
            function(d) {
              //銷毀Bitmap圖片
              bitmap.clear();
              plus.nativeUI.alert("保存圖片到相冊成功");
            },
            function(e) {
              //銷毀Bitmap圖片
              bitmap.clear();
              plus.nativeUI.alert("保存到系統相冊失敗,請嘗試重新保存");
            }
          );
        },
        function() {
          plus.nativeUI.alert("保存失敗,請嘗試重新保存");
        }
      );
    },
 
 
 
 


免責聲明!

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



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