uniapp h5 小程序点击按钮将图片保存到本地

小程序:
思路:向用户发起授权请求,再保存到本地。如果没有授权就打开设置选项让用户重新授权
参考地址:https://blog.csdn.net/weixin_47209386/article/details/120429464

			uni.authorize({
					/* scope.writePhotosAlbum 类型是保存到相册 */
					scope: 'scope.writePhotosAlbum',
					success() {
						/* 已授权进入 */
						/* 保存图片到相册方法方法 */
						/* 获取图片的信息 */
						uni.getImageInfo({
							src:this.imgUrl, 
							success: function(image) {
								/* 保存图片到手机相册 */
								uni.saveImageToPhotosAlbum({
									filePath: image.path,
									success: function() {
										uni.showModal({
											title: '保存成功',
											content: '图片已成功保存到相册',
											showCancel: false
										});
									},
									complete(res) {
										console.log(res);
									}
								});
							},
							fail: function(image){
								console.log(this.imageUrl);
							}
						});
					},
					complete(res) {
						/* 判断如果没有授权就打开设置选项让用户重新授权 */
						uni.getSetting({
							success(res) {
								if (!res.authSetting['scope.writePhotosAlbum']) {
									/* 打开设置的方法 */
									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('用户不授权');
														}
													}
												});
											}
										}
									});
								}
							}
						});
					}
				})

H5:
uni.saveImageToPhotosAlbum不支持H5,因此不能用这个方法
在这里插入图片描述
思路:利用a标签的dowload下载图片
参考地址:https://blog.csdn.net/weixin_50606255/article/details/119609867

var oA = document.createElement("a"); // 创建一个a标签
// 正则表达式,这里是把图片文件名分离出来。拿到文件名赋到a.download,作为文件名来使用文本 ,
// a的download 谷歌浏览器必须同源才能强制下载,否则跳转到图片地址
oA.download = this.imgUrl.replace(/(.*\/)*([^.]+.*)/ig,"$2").split("?")[0]; ; // 设置下载的文件名,默认是'下载'
oA.href = this.imgUrl;
document.body.appendChild(oA);
oA.click();
oA.remove(); // 下载之后把创建的元素删除
  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
UniApp是一种基于Vue.js开发的跨平台应用框架,可以同时开发iOS、Android、H5和小程序等多个平台的应用。UniApp小程序是指使用UniApp框架开发的小程序。 要实现在UniApp小程序中点截屏的功能,可以按照以下步骤进行操作: 1. 在需要添加截屏功能的页面中,添加一个按元素,例如: ```html <template> <view> <button @click="captureScreen">点截屏</button> </view> </template> ``` 2. 在页面的methods中定义captureScreen方法,用于触发截屏操作: ```javascript methods: { captureScreen() { uni.showToast({ title: '截屏中...', icon: 'loading', duration: 2000 }); // 使用uni-app提供的api进行截屏操作 uni.canvasToTempFilePath({ x: 0, y: 0, width: uni.getSystemInfoSync().windowWidth, height: uni.getSystemInfoSync().windowHeight, destWidth: uni.getSystemInfoSync().windowWidth, destHeight: uni.getSystemInfoSync().windowHeight, canvasId: 'canvas', success: (res) => { // 截屏成功后的回调函数 uni.hideToast(); console.log(res.tempFilePath); // 截屏成功后的临时文件路径 // 可以将截屏后的图片路径保存到本地或上传到服务器等操作 }, fail: (err) => { // 截屏失败后的回调函数 uni.hideToast(); console.log(err); } }, this); } } ``` 3. 在页面的wxml中添加一个canvas元素,并设置id为"canvas",用于截取屏幕内容: ```html <template> <view> <button @click="captureScreen">点截屏</button> <canvas id="canvas" style="display: none;"></canvas> </view> </template> ``` 这样,当用户点时,就会触发截屏操作,并将截屏后的图片路径输出到控制台。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值