微信小程序替换微信头像

需要wx.chooseImage这个API和bindTap事件修改。

bindViewTap:function(){
    var that=this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFilePaths
        //console.log(tempFilePaths)
        that.setData({avatarUrl:tempFilePaths[0]})//
      }
    })
  },

注意索引从0开始,count代表最多可选取图片数量,选图图片下标从0开始

Page下的data中定义默认的头像即

avatarUrl:"../../images/1.png"

这是wxml中的代码:

  <image bindtap="bindViewTap" class="userinfo-avatar" src="{{avatarUrl}}" mode="cover"></image>

猜你喜欢

转载自blog.csdn.net/qq_42785280/article/details/99292568
今日推荐