微信小程序:上传头像并裁剪功能相关代码

本文介绍了如何在微信小程序中实现头像上传及裁剪功能。首先通过小程序接口从相册或相机获取图片,接着在新的页面上利用HTML5的canvas元素和特定组件完成图片裁剪。完整代码包括js和html部分,测试后证实功能正常。最后提供了组件的下载链接。
摘要由CSDN通过智能技术生成

首先从相册或者拍照选择图片
js代码:

//点击上传头像方法
  showAction() {
   
    let that = this
    wx.showActionSheet({
   
      itemList: ['从相册中选择', '拍照'],
      success: function(res) {
   
        if (!res.cancel) {
   
          console.log(res.tapIndex)
          if (res.tapIndex == 0) {
   
            that.chooseWxImage('album')
          } else if (res.tapIndex == 1) {
   
            that.chooseWxImage('camera')
          }
        }
      }
    })
  },
  chooseWxImage: function(type) {
   
    var that = this;
    wx.chooseImage({
   
      count:1,
      sizeType: ['original', 'compressed'],
      sourceType: [type],
      success: function(res) {
   
        console.log(res);
        // that.setData({
   
        //   // tempFile   ath可以作为img标签的src属性显示图片
        //   avatar: res.tempFilePaths[0],
        // })
        var tempFilePaths = res.tempFilePaths;
        wx.navigateTo({
   
          url: "/pages/avatarCut/index?src=" + tempFilePaths
        });
      }
    })
  },

然后新建一个页面,放置画布等,对裁剪图片进行操作
这个地方需要引入一个组件,当如小程序文件中的components文件夹里就可以,文章尾部会有放置下载地址

html代码:

//引入的组件
<import src="../../components/we-cropper/we-cropper.wxml"/>
<view class="cropper-wrapper">
  <template is="we-cropper" data="{
   {...cropperOpt}}"/>
  <view class="cropper-buttons">
    <canvas canvas-id='attendCanvasId' class='myCanvas' style
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值