微信小程序为指定图片换照片

//index.js
//获取应用实例
const app = getApp()
/**index.js**/
Page({
  data: {
    imgpath:"../../images/1.png"
  },
  ViewTap:function(){
    var that=this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFilePaths
        that.setData({ imgpath: tempFilePaths[0] })
      }
    })
  },
})

不同文件

<!--index.wxml-->
  <view class="apidemo">
    <image src="{{imgpath}}" class="img" bindtap="ViewTap"></image>
    <image src="../../images/4.png" class="img"></image>
    <image src="../../images/5.png" class="img"></image>
  </view>

不同文件

/**index.wxss**/
.apidemo{
  display:flex;
  flex-direction:column;
  align-items:center;
}
.img{
  height:120rpx;
  width:120rpx;
}

在这里插入图片描述

猜你喜欢

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