uni-app 从本地相册选择图片或使用相机拍照

终于弄懂这个选择图片的流程,删除了很多不需要的地方,最终得到的就是这个版本
页面部分
<view>上传图片  {{imageList.length}}/{{count}}</view>
<view class="uni-list list-pd">
	<view class="uni-uploader__files">
		<block v-for="(image,index) in imageList" :key="index">
			<view class="uni-uploader__file">
				<image class="uni-uploader__img" :src="image" :data-src="image" @tap="previewImage"></image>
			</view>
		</block>
		<view class="uni-uploader__input-box">
			<view class="uni-uploader__input" @tap="chooseImage"></view>
		</view>
	</view>
</view>
script部分
export default {
		data() {
			return {
				imageList:[],
				count: 3,   //设置能上传的最大数量
			}
		},
		methods: {
		/* ---------------上传图片--------------- */
		chooseImage: async function() {
				const _self = this
				if (_self.imageList.length === _self.count) {
					let isContinue = await _self.isFullImg();
					console.log("是否继续?", isContinue);
					if (!isContinue) {
						return;
					}
				}
				/* 选中图片 */
				uni.chooseImage({
					sourceType: ['camera', 'album'],
					sizeType: ['original', 'compressed'],
					count: _self.count,
					success: (res) => {
						const tempFilePaths = res.tempFilePaths; //获取到本地图片地址
						/* 上传图片 */
						const uploadTask = uni.uploadFile({
						      url : 'http://*********',
						      filePath: tempFilePaths[0],
						      name: 'file',
							  header:{
									'Authorization':  token
							  },
						      formData: {
									'user': 'test',
						      },
						      success: function (res) {
							   let toJsonRes = JSON.parse(res.data) //获取到后台处理过的地址
							   let list = []
							   list.push( "http://*********"+toJsonRes.data)
							   _self.imageList = _self.imageList.concat(list);
							  }
						});
					}
				})
			},
			/* 图片数量满了弹出窗口询问是否清空 */
			isFullImg: function() {
				return new Promise((res) => {
					uni.showModal({
						content: "已经有"+this.count+"张图片了,是否清空现有图片?",
						success: (e) => {
							if (e.confirm) {
								this.imageList = [];
							} else {
								
							}
						},
						fail: () => {
							res(false)
						}
					})
				})
			},
			/* 预览图片 */
			previewImage: function(e) {
				let current = e.target.dataset.src
				uni.previewImage({
					current: current,
					urls: this.imageList
				})
			},
	
			/* 删除图片 */
			delImage(e) {
				uni.showModal({
					title: '删除照片',
					content: '确定要删除这张照片吗?',
					cancelText: '取消',
					confirmText: '确定',
					success: res => {
						if (res.confirm) {
							this.imageList.splice(e.currentTarget.dataset.index, 1);
						}
					}
				})
			},
			

在这里插入图片描述
在这里插入图片描述

详细可查看官方文档
从本地相册选择图片或使用相机拍照

  • 5
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值