微信小程序 本地背景图片设置

微信小程序 本地背景图片设置,第1张

1、背景图片设置可以用服务器上的图片。

2、也可以将本地图片转成base64的。

wxml:

<view class="topview-left" style="background-image: url({{background}})mode='scaleToFill'"/>

js:

data: {

    background: "/style/images/icon_coupon_backgroud.png",

  },

/**

  * 生命周期函数--监听页面加载

  */

  onLoad: function(options) {

    //设置背景图片

    let base64 = wx.getFileSystemManager().readFileSync(this.data.background, 'base64')

    this.setData({

      'background': 'data:image/pngbase64,' + base64

    })

    //设置导航栏标题

    wx.setNavigationBarTitle({

      title: '下发优惠券'

    })

  },

wxss:

.topview-left {

  display: flex

  flex-direction: column

  align-items: center

  justify-content: center

  width: 30%

  height: 113px

  background-repeat: no-repeat/** 不重复*/

  background-size: 100% 100%

  background-image: url('data:image/pngbase64,base64码')/** 添加背景图片的*/

}

3、也可以直接设置定位实现。

         <view style="display: flexalign-items: centertext-align:centerjustify-content: centermargin-bottom:10px">

               <image src="/pagesChronic/images/icon_case_background.png" style="height:26pxwidth:167pxposition:absolute"></image>

                <text style="position: relativecolor:#FF9721">评价</text>

           </view>

效果:

微信小程序 拍照和相机选择详解

前言:

小程序中获取图片可通过两种方式得到,第一种是直接打开微信内部自己的样式,第一格就是相机拍照,后面是图片,第二种是d框提示用户是要拍照还是从相册选择,下面一一来看。

选择相册要用到wx.chooseImage(OBJECT)函数,具体参数如下:

直接来看打开相机相册的代码:

Page({   data: {    tempFilePaths: ''   },   onLoad: function () {   },   chooseimage: function () {    var that = this   wx.chooseImage({     count: 1, // 默认9      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有      success: function (res) {      // 返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片       that.setData({       tempFilePaths: res.tempFilePaths      })     }    })     },          })      

方法一效果图如下:

个人认为第二种用户体验要好一点,效果如下:

点击获取d框提示,代码如下:

Page({   data: {    tempFilePaths: ''   },   onLoad: function () {   },   chooseimage: function () {    var that = this   wx.showActionSheet({     itemList: ['从相册中选择', '拍照'],     itemColor: "#CED63A",     success: function (res) {      if (!res.cancel) {       if (res.tapIndex == 0) {        that.chooseWxImage('album')       } else if (res.tapIndex == 1) {        that.chooseWxImage('camera')       }      }     }    })     },     chooseWxImage: function (type) {    var that = this   wx.chooseImage({     sizeType: ['original', 'compressed'],     sourceType: [type],     success: function (res) {      console.log(res)     that.setData({       tempFilePaths: res.tempFilePaths[0],      })     }    })   }      })      

文件的临时路径,在小程序本次启动期间可以正常使用,如需持久保存,需在主动调用 wx.saveFile,在小程序下次启动时才能访问得到。

布局文件:

<button style="margin:30rpx" bindtap="chooseimage">获取图片</button> <image src="{{tempFilePaths }}" catchTap="chooseImageTap" mode="aspectFit" style="width: 100%height: 450rpx" />   

首先需获取scope.writePhotosAlbum权限。(放在wx.getSetting之前调用,建议放在onload中)

整理一下,处理用户拒绝授权的情况

授权完成后执行保存图片,先看看微信的开发文档,需注意我用红框框起来的地方。

我说的两种方法一种是保存临时文件路径的图片,另一种是保存的永久文件的路径,看完两种方法怎样使用看个人选择。

还有一点需注意的是不可以使用网络图片路径,否则保存图片失败,提示路径错误,找不到图片路径。

永久图片路径即保存在微信小程序项目中的图片,例如:'imgs/index/1.png'

页面中给图片标签一个点击事件,绑定保存图片的方法,我把js方法贴上,非常简单。

先进行授权,授权成功后调用微信api即可

这个就有点麻烦,需调用wx.getImageInfo或者wx.downloadFile,而调用这个api需在后台进行配置,把需用到的图片接口添加进去。

具体配置,登录微信公众平台,在开发---开发设置---服务器域名---添加downloadFile合法域名。

有两种写法:


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/7709897.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-08
下一篇 2023-04-08

发表评论

登录后才能评论

评论列表(0条)

保存