需要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>