uniapp微信小程序保存base64图片的方法

uniapp hykeda 79℃ 0评论

使用uni.getFileSystemManager().writeFile去下载base64图片

<view class="share-btn" @click="saveAlbum()">保存相册分享</view>

//保存相册
            saveAlbum(){
                uni.getSetting({//获取用户的当前设置
                    success:(res)=> {
                        if(res.authSetting['scope.writePhotosAlbum']){//验证用户是否授权可以访问相册
                            this.saveImageToPhotosAlbum();
                        }else{
                            uni.authorize({//如果没有授权,向用户发起请求
                                scope: 'scope.writePhotosAlbum',
                                success:()=> {
                                    this.saveImageToPhotosAlbum();
                                },
                                fail:()=>{
                                    uni.showToast({
                                        title:"请打开保存相册权限,再点击保存相册分享",
                                        icon:"none",
                                        duration:3000
                                    });
                                    setTimeout(()=>{
                                        uni.openSetting({//调起客户端小程序设置界面,让用户开启访问相册
                                            success:(res2)=> {
                                                // console.log(res2.authSetting)
                                            }
                                        });
                                    },3000);
                                }
                            })
                        }
                    }
                })

            },
            saveImageToPhotosAlbum(){
                let base64=this.qrcode.replace(/^data:image\/\w+;base64,/, "");//去掉data:image/png;base64,
                let filePath=wx.env.USER_DATA_PATH + '/hym_pay_qrcode.png';
                uni.getFileSystemManager().writeFile({
                    filePath:filePath ,  //创建一个临时文件名
                    data: base64,    //写入的文本或二进制数据
                    encoding: 'base64',  //写入当前文件的字符编码
                    success: res => {
                        uni.saveImageToPhotosAlbum({
                            filePath: filePath,
                            success: function(res2) {
                                uni.showToast({
                                    title: '保存成功,请从相册选择再分享',
                                    icon:"none",
                                    duration:5000
                                })
                            },
                            fail: function(err) {
                                // console.log(err.errMsg);
                            }
                        })
                    },
                    fail: err => {
                        //console.log(err)
                    }
                })
            }
        },

如果是直接远程图片:

uni.getImageInfo({

src: path, //远程地址

success: function (image) {

uni.saveImageToPhotosAlbum({

filePath: image.path,//返回图片的本地路径

success() {

that.$common.showToast('下载成功')

},

fail(res) {

console.log(path)

console.log(res);

that.$common.showToast(res)

},

complete() {

// console.log(image.path)

}

});

}

});

转载请注明:雕木乱 » uniapp微信小程序保存base64图片的方法

喜欢 (6)

您必须 登录 才能发表评论!