uniapp 保存图片到相册(小程序版)

一开始我还还以为挺简单的,毕竟文档这么多,但是就是保存不了 经过半天的摸索 有点思路

我用的是uni-app,如果原生小程序就把uni改成wx

第一步点击判断是否授权 执行对应方法

preservationImg(img) {
	let that = this
	uni.authorize({
        /* scope.writePhotosAlbum 类型是保存到相册 */
		scope: 'scope.writePhotosAlbum',
		success() {
            /* 已授权进入 */
			/* 保存图片到相册方法方法 */
			that.imgApi(img);
		},
		complete(res) {
			/* 判断如果没有授权就打开设置选项让用户重新授权 */
		    uni.getSetting({
				success(res) {
				    if (!res.authSetting['scope.writePhotosAlbum']) {
				       /* 打开设置的方法 */
				        that.openInstall();
				    }
				}
		    });
	    }
	});
}

保存图片方法

         imgApi(image) {
			    /* 获取图片的信息 */
			    uni.getImageInfo({
			        src: image,
			        success: function(image) {
			            /* 保存图片到手机相册 */
			            uni.saveImageToPhotosAlbum({
			                filePath: image.path,
			                success: function() {
			                    uni.showModal({
			                        title: '保存成功',
			                        content: '图片已成功保存到相册',
			                        showCancel: false
			                    });
			                },
			                complete(res) {
			                    console.log(res);
			                }
			            });
			        }
			    });
			},

跳转设置重新授权方法

            opensit() {
			    uni.showModal({
			        content: '没有授权保存图片到相册,点击确定去允许授权',
			        success: function(res) {
			            if (res.confirm) {
			                /* 打开设置的API*/
			                uni.openSetting({
			                    success(res) {
			                        console.log(res.authSetting);
			                    }
			                });
			            } else if (res.cancel) {
			                uni.showModal({
			                    cancelText: '取消',
			                    confirmText: '重新授权',
			                    content: '你点击了取消,将无法进行保存操作',
			                    success: function(res) {
			                        if (res.confirm) {
			                            uni.openSetting({
			                                success(res) {
                                                /* 授权成功 */
			                                    console.log(res.authSetting);
			                                }
			                            });
			                        } else if (res.cancel) {
			                            console.log('用户不授权');
			                        }
			                    }
			                });
			            }
			        }
			    });
			},

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值