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

你。 提交于 2019-11-27 02:47:42
//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;
}

在这里插入图片描述

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!