微信小程序保存canvas繪製的圖片到本地

canvasToImage() { //  canvas畫布轉成圖片
        var that = this;
        wx.canvasToTempFilePath({
            quality: 1,
            fileType: 'jpg',
            canvasId: 'mycanvas',
            success: function(res) {
                wx.hideLoading();
                console.log('圖片地址' + res.tempFilePath);
                that.setData({
                    img_temp: res.tempFilePath
                })
                // wx.previewImage({
                //   current: res.tempFilePath, // 當前顯示圖片的http鏈接
                //   urls: [res.tempFilePath] // 需要預覽的圖片http鏈接列表
                // })
                wx.saveImageToPhotosAlbum({
                    filePath: res.tempFilePath,
                    success(res) {
                        console.log(res);
                        wx.showModal({
                            title: '',
                            content: '圖片已保存到相冊',
                            showCancel: false,
                            confirmText: '好的',
                            confirmColor: '#72B9C3',
                            success: function(res) {
                                if (res.confirm) {
                                    console.log('用戶點擊確定');
                                }
                            }
                        })
                    },
                    fail: function(res) {
                        if (res.errMsg === "saveImageToPhotosAlbum:fail auth deny") {
                            wx.openSetting({
                                success(settingdata) {
                                    console.log(settingdata)
                                    if (settingdata.authSetting['scope.writePhotosAlbum']) {
                                        that.saveImg(); //保存失敗嘗試再次保存
                                    } else {
                                        console.log('獲取權限失敗,給出不給權限就無法正常使用的提示')
                                    }
                                }
                            })
                        }
                    }
                })

            },
            fail: function(res) {
                console.log(res)
            }
        }, this)
    },

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章