首页 > 编程语言 >uni-app:微信小程序:保存图片到相册(hbuilderx 3.7.3)

uni-app:微信小程序:保存图片到相册(hbuilderx 3.7.3)

时间:2023-02-25 09:11:25浏览次数:51  
标签:相册 title url 微信 app hideLoading 3.7 res uni

一,代码:

<template>
    <view>
        <view style="margin-left: 20rpx;margin-top: 20rpx; width:710rpx;height:710rpx;background: gray;">
        <image class="banner" mode="aspectFit"  :show-menu-by-longpress="true"  
        src="https://imgs-qn.iliangcang.com/ware/slider/1908.jpg" />
        </view>

        <button class="shareBtn" type="default" @click="savePosterPath('https://imgs-qn.iliangcang.com/ware/slider/1908.jpg')"> 
                        保存
        </button>
    </view>
</template>

<script>
    export default {
        data() {
            return {
                
            }
        },
        methods: {
            savePosterPath(url){
                let that = this;
                uni.showLoading({
                    title: '正在保存图片...'
                });
                //获取用户的当前设置。获取相册权限
                uni.getSetting({
                    success: (res) => {
                        //如果没有相册权限
                        if (!res.authSetting["scope.writePhotosAlbum"]) {
                            //向用户发起授权请求
                            uni.authorize({
                                scope: "scope.writePhotosAlbum",
                                success: () => {
                                    //授权成功保存图片到系统相册
uni.hideLoading(); that.saveNetImageToLocal(url); }, //授权失败 fail: () => { uni.hideLoading(); uni.showToast({ title: "未授权保存图片到相册!", }); }, }); } else { that.saveNetImageToLocal(url); } }, fail: (res) => {}, }); },
//保存网络图片到本地 saveNetImageToLocal(url){ uni.downloadFile({ url:url, success:(res)=>{ if (res.statusCode === 200) { uni.saveImageToPhotosAlbum({ filePath: res.tempFilePath, success: (res) => { uni.hideLoading(); return uni.showToast({ title: "保存成功!", }); }, fail: (res) => { uni.hideLoading(); console.log(res.errMsg); return uni.showToast({ title: res.errMsg, }); }, //无论成功失败都走的回调 complete: (res) => {uni.hideLoading();}, }); } else { uni.showToast({ title: '网络错误', }); } } }); }, } } </script> <style> .banner { width:100%; height:100%; } </style>

 

说明:刘宏缔的架构森林是一个专注架构的博客,地址:https://www.cnblogs.com/architectforest

         对应的源码可以访问这里获取: https://github.com/liuhongdi/
         或: https://gitee.com/liuhongdi

说明:作者:刘宏缔 邮箱: [email protected]

二,可能出现的报错信息:

                                    //授权成功保存图片到系统相册
                                    uni.saveImageToPhotosAlbum({
                                        //图片路径,不支持网络图片路径
                                        filePath: url,
                                        success: (res) => {
                                            uni.hideLoading();
                                            return uni.showToast({
                                                title: "保存成功!",
                                            });
                                        },
                                        fail: (res) => {
                                            console.log(res.errMsg);
                                            return uni.showToast({
                                                title: res.errMsg,
                                            });
                                        },
                                        complete: (res) => {uni.hideLoading();},
                                    });

这里需要注意的是:如果直接传递网络图片的地址给filePath,则系统会报错如下:

saveImageToPhotosAlbum:fail https://img.lhdtest.com/ware/slider/1908.jpg not absolute path 

所以在实际应用中需要传递的是一个本地文件地址

三,测试效果

四,查看hbuilderx的版本: 

标签:相册,title,url,微信,app,hideLoading,3.7,res,uni
From: https://www.cnblogs.com/architectforest/p/17153744.html

相关文章

  • Android 观察App运行日志和面对安装工程中需要在
    Android采用Log工具打印日志,它将各类日志划分为五个等级;log.e表示错误信息,比如可能导致程序崩溃的异常log.w表示警告信息log.i表示一般消息log.d表示调试信息。可把程......
  • UNI——APP开发
    今天我们开启心得学习之路,有关于APP开发的相关知识,是在一个名为hbuilderX的软件上面进行开发,可以开发h5程序,微信小程序,以及适配与安卓的APP。首先关于APP的安装很简......
  • 【1】部署环境python+pycharm+JDK+SDK+node安装+appium安装+Appium-python-client安装
                                               安装SDK检查命令 adbdoctor......
  • 打卡App关于每日总结打卡内容的实现(大概,写的巨烂,惨不忍睹
      首先我的项目结构是登录页面、注册页面、添加打卡信息页面、打卡信息展示页面关于坚持天数和连续坚持的天数,我这里使用了SharePrefence共享参数的方式,导致代码很冗......
  • uniapp(1)
    **在项目根目录中新建.gitignore忽略文件,并配置如下:忽略node_modules/node_modules/unpackage/dist**添加页面新建页面,而后选择scss模板;即可在pages.json看到所添加......
  • app项目简历--参考
    app技能1、通过ADB命令抓取日志,对测试中遇到的问题做初步分析,作为提单的附件;2、完成app端的功能测试、接口测试和专项测试3、对APP进行稳定性测试(adb),弱网测试(fiddler......
  • 找不到AppleID密码的iPhone
    几年前买了一个iPhone6,刚用不久就黑屏了。在维修期间就拿公司的测试设备来用,在维修的那几天中因为频繁的切换设备登录QQ,导致QQ账号异常被封掉了。起初对于QQ被封了,不怎么在......
  • uni-app:自定义顶部导航栏(hbuilderx 3.7.3)
    一,代码:说明:我们使整个顶部导航栏透明,只保留一个退回上一页的按钮模板<!--自定义导航栏--><viewclass="navBarBox"style="position:fixed;top:0;z-index:......
  • 理解JS函数之call,apply,bind
    前言在JavaScript中,apply、bind和call是三个重要的函数,它们都是Function.prototype的方法。这些函数可以让我们动态地改变函数的this值,或者传递参数来执行函数。......
  • uni-app:获取当前经纬度解决方案+如何布置全局组件
    这里给大家分享我在网上总结出来的一些知识,希望对大家有所帮助 一.布置全局组件在我们开发的过程中,会碰到一个现象,就是在页面里面引入组件,总算要写import,components才......