// 保存图片
saveImage() {
let that = this;
// 向用户发起授权请求
uni.authorize({
scope: 'scope.writePhotosAlbum',
success: () => {
// 已授权,调用保存图片方法
that.handleSaveImg ();
},
fail: () => {
// 拒绝授权,获取当前设置
uni.getSetting({
success: (result) => {
if (!result.authSetting['scope.writePhotosAlbum']) {
// 开启授权
that.isAuth()
}
}
});
}
})
},
// 引导用户开启权限
isAuth() {
uni.showModal({
content: '由于您还没有允许保存图片到您相册里,无法进行保存,请点击确定允许授权',
success: (res) => {
if (res.confirm) {
uni.openSetting({
success: (result) => {
console.log(result.authSetting);
}
});
}
}
});
},
// 保存图片
handleSaveImg () {
// 由于saveImageToPhotosAlbum不支持网络图片路径,所以要通过downloadFile下载文件资源到本地
uni.downloadFile({
url: 'https://www.example.com/upload', //仅为示例,非真实的接口地址
success: (res) => {
if (res.statusCode === 200) {
uni.saveImageToPhotosAlbum({
filePath: res.tempFilePath,
success: () => {
this.toast('保存成功')
}
});
}
}
});
},
uni 保存图片(判断用户是否授权)
于 2022-11-02 15:29:28 首次发布