wx上传图片以及转base64

wx图片

  • 转base64
piceImg(){
	this.pics = []
	let that = this
	const fileManager = wx.getFileSystemManager();
	// that.imageList图片数组[{piceUrl:''}]
	for(let rfd of that.imageList){
		// console.log(rfd.piceUrl)
		// 判断是本地上传的还是https回显的
		if(rfd.piceUrl && rfd.piceUrl.split('/upload')[1]){
			// https回显
			wx.downloadFile({
				url: rfd.piceUrl,
				success(res) {
					// console.log(res.tempFilePath)
					let base64 = fileManager.readFileSync(res.tempFilePath, 'base64');
					that.pics.push('data:image/png;base64,'+base64+')')
				},
				fail(res){
					uni.showToast({
						title: '图片不在域列表中'+JSON.stringify(res),
						icon: 'none'
					})
				}
			})
			// that.pics.push(rfd.piceUrl)
		} else if(rfd.piceUrl) {
			// 本地上传
			let base64 = fileManager.readFileSync(rfd.piceUrl, 'base64');
			that.pics.push('data:image/png;base64,'+base64+')')
		}
	}
	// console.log(that.pics)
}
  • 查看图片
previewImage(index) {
	let imgs = []
	this.imageList.forEach(item=>{
		// 判断是否有图片
		if(item.piceUrl){
			imgs.push(item.piceUrl)
		}
	})
	uni.previewImage({
		current: index,// 下标1,就是重1开始
		urls: imgs
	})
}
  • 上传图片
    图片显示数组imageList
addPicture(index){
	let that = this;
	uni.chooseImage({
		count: 3, //默认9,最多上传数量,最多可一次选3张
		sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
		sourceType: ['album', 'camera'], //从相册选择
		success: function (res) {
			// 第1个上传框(最多三个框)
			if(index == 0){
				that.imageList[0].piceUrl = res.tempFilePaths[0]
				if(res.tempFilePaths.length == 1 || res.tempFilePaths.length == 2){
					that.imageList.push({piceUrl:''})
				}else{
					for(let i in res.tempFilePaths){
						if(i>0){
							that.imageList.push({piceUrl:res.tempFilePaths[i]})
						}
					}
				}
			}else if(index == 1){// 第2个上传框
				that.imageList[1].piceUrl = res.tempFilePaths[0];
				if(res.tempFilePaths.length>1){
					that.imageList.push({piceUrl:res.tempFilePaths[1]})
				}else if(res.tempFilePaths.length == 1){
					that.imageList.push({piceUrl:''})
				}
			}else{
				that.imageList[index].piceUrl = res.tempFilePaths[0];
			}
			that.piceImg()// 转base64
		},
		fail () {
		    wx.showToast({
		      title: '获取图片失败',
			  icon: 'none',
		      duration: 2000
		    })
		}
	});
}
  • 图片单个删除
imageDel(index) {
	this.imageList.splice(index,1)
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值