uniApp实现选择图片裁剪设置用户头像

官方demo里有图片裁剪的功能,所以这里直接拿来用了,uniApp——GitHub,效果可以扫下链接里的小程序码(模板-图片裁剪)看下,这块代码位置在pages/template/crop/crop.vue。

//这里要实现的功能是裁剪图片修改用户头像,所以要调用接口上传到服务器。
//这里用到的until.方法都是自己封装或基于原uniapp方法封装的,功能语义化
// 获取图片
getImageInfo() {
	var _this = this;
	uni.showLoading({
		title: '图片生成中...',
	});
	// 将图片写入画布
	const ctx = uni.createCanvasContext('myCanvas');
	ctx.drawImage(_this.imageSrc, 0, 0, IMG_REAL_W, IMG_REAL_H);
	ctx.draw(true, () => {
		// 获取画布要裁剪的位置和宽度   均为百分比 * 画布中图片的宽度    保证了在微信小程序中裁剪的图片模糊  位置不对的问题 canvasT = (_this.cutT / _this.cropperH) * (_this.imageH / pixelRatio)
		var canvasW = ((_this.cropperW - _this.cutL - _this.cutR) / _this.cropperW) * IMG_REAL_W;
		var canvasH = ((_this.cropperH - _this.cutT - _this.cutB) / _this.cropperH) * IMG_REAL_H;
		var canvasL = (_this.cutL / _this.cropperW) * IMG_REAL_W;
		var canvasT = (_this.cutT / _this.cropperH) * IMG_REAL_H;
		uni.canvasToTempFilePath({
			x: canvasL,
			y: canvasT,
			width: canvasW,
			height: canvasH,
			destWidth: canvasW,
			destHeight: canvasH,
			quality: 0.5,
			canvasId: 'myCanvas',
			success: function (res) {
				uni.hideLoading()
				uni.uploadFile({
					url : until.domain() + 'home/upload/upload',
					filePath:res.tempFilePath,
					name: 'file',
					success : function(res){
						//upload方法返回回来的数据是string类型,所以这里要转成json对象
						let result = JSON.parse(res.data)
						if(result.status == 1){
							console.log("进入")
							until.ly_request({
								url : 'user/center/avatar',
								method : 'post',
								data : {
									avatar: {
										src : result.id
									},
									nocrop : true
								},
								success : function(res){
									// console.log(JSON.stringify(res))
									if(res.data.status == 1){
										uni.showToast({title:'头像修改成功'})
										setTimeout(function(){uni.navigateBack({delta:2})},1000)
									}
									until.tips(res)
								}
							})
						}
					}
				})
			}
		});
	});
},

另外这边的需求是设置1:1裁剪,不可拉伸

//注释掉以下部分关闭图片裁剪的拉伸
<!-- 	<view class="uni-cropper-line-t" data-drag="top" @touchstart.stop="dragStart" @touchmove.stop="dragMove"></view>
		<view class="uni-cropper-line-r" data-drag="right" @touchstart.stop="dragStart" @touchmove.stop="dragMove"></view>
		<view class="uni-cropper-line-b" data-drag="bottom" @touchstart.stop="dragStart" @touchmove.stop="dragMove"></view>
		<view class="uni-cropper-line-l" data-drag="left" @touchstart.stop="dragStart" @touchmove.stop="dragMove"></view>
		<view class="uni-cropper-point point-t" data-drag="top" @touchstart.stop="dragStart" @touchmove.stop="dragMove"></view> -->
								
<!-- 	<view class="uni-cropper-point point-r" data-drag="right" @touchstart.stop="dragStart" @touchmove.stop="dragMove"></view>
		<view class="uni-cropper-point point-rb" data-drag="rightBottom" @touchstart.stop="dragStart" @touchmove.stop="dragMove"></view>
		<view class="uni-cropper-point point-b" data-drag="bottom" @touchstart.stop="dragStart" @touchmove.stop="dragMove" @touchend.stop="dragEnd"></view>
		<view class="uni-cropper-point point-bl" data-drag="bottomLeft"></view>
		<view class="uni-cropper-point point-l" data-drag="left" @touchstart.stop="dragStart" @touchmove.stop="dragMove"></view> -->

// 设置大小的时候触发的touchStart事件
// 			dragStart(e)
// 设置大小的时候触发的touchMove事件
// 			dragMove(e) 

另外,在loadImage里面的uni.getImageInfo方法中将裁剪比例修改成以下值

// IMG_RATIO = res.width / res.height
IMG_RATIO = 1 / 1
  • 4
    点赞
  • 44
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Uniapp可以通过引入第三方插件来实现图片裁剪功能,例如使用vue-cropper插件。 1. 安装vue-cropper插件 在项目根目录下打开命令行,输入以下命令安装vue-cropper插件。 ``` npm install vue-cropper ``` 2. 在页面中引入vue-cropper组件 在需要使用图片裁剪功能的页面中,引入vue-cropper组件。 ```html <template> <div> <vue-cropper ref="cropper" :img="img" :outputSize="{width: 300, height: 300}" :outputType="'jpeg'" :autoCrop="'true'" :canMove="'true'" :canZoom="'true'" :center="'true'" ></vue-cropper> <button @click="cropImage">裁剪图片</button> </div> </template> <script> import VueCropper from 'vue-cropper' export default { components: { VueCropper }, data() { return { img: '' } }, methods: { cropImage() { this.$refs.cropper.getCroppedCanvas().toBlob((blob) => { // 处理裁剪后的图片blob数据 }) } } } </script> ``` 3. 配置vue-cropper组件参数 在引入vue-cropper组件时,可以设置各种参数来达到自己的需求。常见的参数如下: - img:待裁剪图片地址 - outputSize:输出裁剪图片的尺寸 - outputType:输出裁剪图片的类型 - autoCrop:是否自动裁剪 - canMove:是否可以移动裁剪框 - canZoom:是否可以缩放裁剪框 - center:是否居中显示裁剪框 4. 裁剪图片 在点击裁剪按钮时,调用vue-cropper组件的getCroppedCanvas方法,获取裁剪后的图片blob数据。可以将blob数据上传到服务器或保存到本地。 ```javascript this.$refs.cropper.getCroppedCanvas().toBlob((blob) => { // 处理裁剪后的图片blob数据 }) ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值