当前位置: 首页 > news >正文

uniapp实现微信小程序端图片保存到相册

效果图展示

在这里插入图片描述

安装插件海报画板导入到项目里面,在页面直接使用

在这里插入图片描述

<template><view><button @click="saveToAlbum" class="save-button">保存到相册</button><image :src="path" mode="widthFix" v-if="path" style="width: 100%;"></image><!-- 绘图组件(保持hidden) --><l-painter ref="painter" hidden isCanvasToTempFilePath @success="path = $event"><l-painter-view css="width:100%;line-height:90rpx;background:#efefef;text-align:center;"><l-painter-text text="分享好友得佣金,好友消费后佣金到账" css="color:blue;font-size:32rpx;"></l-painter-text></l-painter-view><l-painter-view css="padding:30rpx;display: flex;flex-direction: column;align-items: flex-start;"><l-painter-image css="width:100rpx;height:100rpx;border-radius:50rpx;" src="/static/logo.png"mode="aspectFill"></l-painter-image><l-painter-text text="一颗葡萄" css="font-size:28rpx;padding-top:20rpx;"></l-painter-text><l-painter-text text="共发布7篇 | 6分钟前发布" css="font-size:22rpx;color:#ccc"></l-painter-text><l-painter-text text="合租-观山湖区-未来方舟;" css="font-size:40rpx;padding-top:20rpx;"></l-painter-text></l-painter-view><l-painter-view css="padding:30rpx;display: flex;flex-wrap:wrap;align-items: flex-start;"><l-painter-image v-for="item in dataObj.house_images" css="width:30%;;border-radius:50rpx;" :src="item"mode="aspectFill"></l-painter-image></l-painter-view></l-painter></view>
</template><script setup>import {ref,onMounted} from 'vue'const path = ref('')const painter = ref(null)const dataObj = ref({"house_images": ["https://renting-upload.oss-cn-chengdu.aliyuncs.com/App/20250707/1751852511187350583.png","https://renting-upload.oss-cn-chengdu.aliyuncs.com/App/20250707/1751852516350361922.png","https://renting-upload.oss-cn-chengdu.aliyuncs.com/App/20250707/175185252116150042.png","https://renting-upload.oss-cn-chengdu.aliyuncs.com/App/20250707/1751852527581221874.png","https://renting-upload.oss-cn-chengdu.aliyuncs.com/App/20250707/1751852533225847091.png","https://renting-upload.oss-cn-chengdu.aliyuncs.com/App/20250707/1751852537888574703.png","https://renting-upload.oss-cn-chengdu.aliyuncs.com/App/20250707/1751852542759268442.png","https://renting-upload.oss-cn-chengdu.aliyuncs.com/App/20250707/1751852547136723025.png","https://renting-upload.oss-cn-chengdu.aliyuncs.com/App/20250707/1751852551059965495.png"]})onMounted(() => {checkAuth()})const checkAuth = () => {uni.getSetting({success(res) {if (!res.authSetting['scope.writePhotosAlbum']) {uni.authorize({scope: 'scope.writePhotosAlbum',success() {console.log('授权成功')},fail() {console.log('授权失败')}})}}})}const saveToAlbum = async () => {if (!path.value) {uni.showToast({title: '图片尚未生成',icon: 'none'})return}try {const tempFilePath = await base64ToTempFilePath(path.value)await uni.saveImageToPhotosAlbum({filePath: tempFilePath})uni.showToast({title: '保存成功',icon: 'success'})} catch (err) {console.error('保存失败:', err)handleSaveError(err)}}const base64ToTempFilePath = (base64) => {return new Promise((resolve, reject) => {const base64Data = base64.replace(/^data:image\/\w+;base64,/, '')const filePath = `${wx.env.USER_DATA_PATH}/${Date.now()}.jpg`wx.getFileSystemManager().writeFile({filePath,data: base64Data,encoding: 'base64',success: () => resolve(filePath),fail: reject})})}const handleSaveError = (err) => {if (err.errMsg.includes('auth deny')) {uni.showModal({title: '提示',content: '需要相册权限才能保存图片',confirmText: '去设置',success: (res) => {if (res.confirm) {uni.openSetting()}}})} else {uni.showToast({title: '保存失败: ' + err.errMsg,icon: 'none'})}}
</script><style lang="scss" scoped>.save-button {margin: 40rpx;background-color: #ff5000;color: white;border-radius: 10rpx;&:active {opacity: 0.8;}}
</style>
http://www.dtcms.com/a/273699.html

相关文章:

  • 深入拆解Spring思想:DI(依赖注入)
  • Python-正则表达式-信息提取-滑动窗口-数据分发-文件加载及分析器-浏览器分析-学习笔记
  • 榫卯企业云平台自服务中心模块(SSC)关键能力解读
  • 数据库报错:Column ‘xxx‘ in field list is ambiguous
  • 2025年体育科学与健康大数据国际会议(ICSSHBD 2025)
  • 在 GitHub 上创建私有仓库
  • 如何在 Windows 10 上安装设置 Apache Kafka
  • hive/spark sql中unix_timestamp 函数的坑以及时间戳相关的转换
  • AI技术正以前所未有的速度重塑职业生态与行业格局,尤其在自动化测试领域,AI驱动的测试框架通过智能化、低代码化重构传统测试流程。
  • PySpark中python环境打包和JAR包依赖
  • spark3 streaming 读kafka写es
  • Google Benchmark 介绍和使用指南
  • 流批一体的“奥卡姆剃刀”:Apache Cloudberry 增量物化视图应用解析
  • CReFT-CAD 笔记 带标注工程图dxf,png数据集
  • 【EGSR2025】材质+扩散模型+神经网络相关论文整理随笔(四)
  • Jenkins 项目类型及配置项
  • FPGA实现SDI转LVDS视频发送,基于GTP+OSERDES2原语架构,提供工程源码和技术支持
  • 资源分享-FPS, 矩阵, 骨骼, 绘制, 自瞄, U3D, UE4逆向辅助实战视频教程
  • 飞算 JavaAI 深度体验:开启 Java 开发智能化新纪元
  • 【拓扑空间】示例及详解4
  • python的社区残障人士服务系统
  • 了解环网式 CAN 转光纤中继器
  • BPE(Byte Pair Encoding)分词算法
  • leetcode-hot100(283.移动零)
  • 政安晨【零基础玩转开源AI项目】ACE-Step —— 迈向音乐生成基础模型的重要一步:AI自动谱曲与自动演唱的免费开源框架部署实践
  • RLHF:人类反馈强化学习 | 对齐AI与人类价值观的核心引擎
  • python实现DoIP基本通信(收发报文)
  • 第十二章:网络编程
  • Typescript -字面量类型
  • Linux的基础I/O