使用 XHbuilder 编辑器 uniapp开发 app 中使用手机本相机可直接拍摄照片进行上传,也可以选择相册进行上传

学习目标:

  • 使用 XHbuilder 编辑器 uniapp开发 app 中使用手机本相机可直接拍摄照片进行上传,也可以选择相册进行上传

学习内容:

相关内容

  1. 上传图片
  2. 上传时调用的相关方法
  3. 配置的相关模块
  4. 需要配置的相关权限

知识小结:

总结:

  • 1、上传图片
<view class="uni-form-item">
					<view class="title">主图</view>
					<view class="content">
						<view class="grid col-4 grid-square flex-sub">
							<view class="bg-img" v-for="(item,index) in imageList" :key="index" @tap="ViewMainImage" :data-url="baseUrl + imageList[index].url">
							 <image :src="baseUrl + item.url" mode="aspectFill"></image>
								<view class="cu-tag bg-red" @tap.stop="DelMainImg" :data-index="index">
									<text class='cuIcon-close'></text>
								</view>
							</view>
							<view class="solids" @tap="ChooseMainImage" v-if="imageList.length<1">
								<text class='cuIcon-camera'></text>
							</view>
						</view>
					</view>
				</view>
  • 2、调用的方法
     //选择图片
	ChooseMainImage() {
			uni.chooseImage({
				count: 10, //默认9
				sizeType: ['original', 'compressed'],  //可以指定是原图还是压缩图,默认二者都有
				sourceType: ['album','camera'],   //从相册选择,也可以使用相机直接拍照上传
				success: (res) => {
					
					var imagePathArr =res.tempFilePaths
					
					uni.showToast({
						title: '上传进度:0/' + imagePathArr.length,
						icon: 'none',
						mask: false
					});
					
					var remoteIndexStart = this.imageList.length - imagePathArr.length	
					var promiseWorkList = []
					var keyname = (this.fileKeyName ? this.fileKeyName : 'file')
					var completeImages = 0
					for (let i = 0; i < imagePathArr.length; i++) {
						
						promiseWorkList.push(new Promise((resolve, reject) => {
							let header = {"Mema-Token":this.$api.config.header["Mema-Token"]};
							let remoteUrlIndex = remoteIndexStart + i;
							
							uni.uploadFile({
								url: this.imageServerUrl,
								// methods: 'POST',
								// fileType: 'image',
								header: header,
								formData: this.uploadDataMain,
								filePath: imagePathArr[i],
								name: keyname,
								success: function(res) {
									
									if (res.statusCode === 200) {
										var jsonData = JSON.parse(res.data);
									
										if(jsonData.code == 0){
											
											completeImages++
											uni.showToast({
												title: '上传进度:' + completeImages + '/' + imagePathArr.length,
												icon: 'none',
												mask: false,
												duration: 500
											});
											resolve(jsonData.data)
										}else{
											uni.showToast({
												icon: 'none',
												position: 'bottom',
												title: jsonData.msg
											});
										}
									} else {
										reject('fail to upload image:' + remoteUrlIndex)
									}
								},
								fail: function(res) {
									reject('fail to upload image:' + remoteUrlIndex)
								}
							})
						}))
					}
					Promise.all(promiseWorkList).then((result) => {
						for (let i = 0; i < result.length; i++) {
							// result[i].url = this.$api.config.baseUrl + result[i].url
							this.imageList.push(result[i])
						}
						
					})
				}
			});
		},
	//查看主图
	ViewMainImage(e) {
		var images = [];
		for (let i = 0; i < this.imageList.length; i++) {
			images.push(this.$api.config.baseUrl + this.imageList[i].url)
		}
		uni.previewImage({
			urls: images,
			current: e.currentTarget.dataset.url
		});
	},
	//删除主图
	DelMainImg(e) {
		var imgObj = this.imageList[e.currentTarget.dataset.index]
		uni.showModal({
			title: '删除图片',
			content: '确定要删除吗?',
			cancelText: '再看看',
			confirmText: '确认删除',
			success: res => {
				if (res.confirm) {
					this.pic=""
					this.imageList = []
				}
			}
		})
	},
  • 3、APP 模块配置
    在 app 模块配置中 ,需勾选 Camera & Gallery(相机和相册) 模块配置
    在这里插入图片描述

  • 4、App 权限配置
    使用 uni-app 相机拍摄照片 需开启相应的权限配置
    1、开启相机权限

(1) <uses-feature android:name=“android.hardware.camera” />
(2) <uses-feature android:name=“android.hardware.camera.autofocus” />

2、 开启闪光灯权限

<uses-permission android:name=“android.permission.FLASHLIGHT”/>

3、开启摄像头权限

<uses-permission android:name=“android.permission.CAMERA” />

在配置勾选框中,勾选相对应的权限配置
在这里插入图片描述
在这里插入图片描述

  • 7
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你好!如果你想使用uniapp开发app,我可以为你提供一些相关信息。Uniapp是一个基于Vue.js的开发框架,它可以将代码编译为多个平台(包括iOS、Android、H5等)。通过使用uniapp,你只需要编写一次代码,就可以在不同平台上运行。 为了开始使用uniapp开发app,你需要安装uni-app开发环境。首先,确保你已经安装了Node.js和Vue CLI(如果你之前没有安装过)。然后,你可以通过以下命令安装uni-app: ``` npm install -g @vue/cli vue create -p dcloudio/uni-preset-vue my-project cd my-project npm run dev ``` 上述命令,我们首先全局安装Vue CLI,然后使用uni-preset-vue创建一个新的uni-app项目,并进入项目目录。最后,通过运行npm run dev命令,你就可以在本地启动一个开发服务器,并在浏览器预览你的app。 在开始开发之前,你需要熟悉Vue.js的基本知识。如果你已经熟悉Vue.js,那么使用uniapp开发app会更加容易上手。 在uniapp,你可以使用Vue的语法编写页面和组件。此外,uniapp还提供了一些平台特定的API和组件,以实现更深入的原生功能。 一旦你完成了app开发,你可以使用uniapp提供的命令来构建app。具体的构建命令将根据你要构建的平台而有所不同。例如,如果你要构建iOS应用,你可以运行以下命令: ``` npm run build:ios ``` 这是一个简单的介绍,希望能帮助到你开始使用uniapp开发app。如果你有进一步的问题,请随提问!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值