nui-app(3)——上传图片以及图片上传成功后预览图片

方法1:

<template>
	<view class="content">
		<view class="text-area">
			<text class="title">{{title}}</text>
			<button type="default" @click="chooseImg">上传图片</button>
		</view>
		<!-- 上传的图片渲染到页面 -->
		<image v-for="item in img" :src="item" mode=""></image>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				title: "首页",
				img: []
			}
		},
		onLoad() {},
		methods: {
			chooseImg() {
			//该方法会自动打开文件,我们就可以上传我们想要的图片
				uni.chooseImage({
					count: 5,//最多可以上传几张图片
					success: res => {
						console.log(res)
						this.img = res.tempFilePaths
					}
				})
			}
		},
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}
	.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}
	.text-area {
		display: flex;
		justify-content: center;
	}
	.title {
		font-size: 36rpx;
		color: #8f8f94;
	}
</style>

效果:
count可以决定我们要上传几张图片(只决定微信小程序的,决定不了浏览器的),浏览器和微信小程序有一点区别。浏览器可上传多张图片并进行预览,但是在微信小程序中打开最多只能一次上传5张图片,具体如下图。

  • 浏览器
    在这里插入图片描述

  • 微信小程序
    在这里插入图片描述

方法2

uni-app默认的方法uni.previewImage,该方法可以选择预览具体某一张图片。代码和效果图如下:

<template>
	<view class="content">
		<view class="text-area">
			<text class="title">{{title}}</text>
			<button type="default" @click="chooseImg">上传图片</button>
		</view>
		<!-- 上传的图片渲染到页面 -->
		<image v-for="item in img" :src="item" mode="" @click="previewImg(item)"></image>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				title: "首页",
				img: []
			}
		},
		onLoad() {},
		methods: {
			chooseImg() {
				uni.chooseImage({
					count: 5,
					success: res => {
						console.log(res)
						this.img = res.tempFilePaths
					}
				})
			},
			previewImg(current){
				console.log(current);
				uni.previewImage({
					current:current,//当前点击的图片
					urls:this.img,//上传的图片
					
					// 注意,下面两个属性只在h5和APP才有用
					loop:true,//自动轮播
					indicator:"number"//default为滑动时底部圆点,number为滑动时为数字
				})
			}
		},
	}
</script>
<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}
	.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}
	.text-area {
		display: flex;
		justify-content: center;
	}
	.title {
		font-size: 36rpx;
		color: #8f8f94;
	}
</style>


在这里插入图片描述
这里我点击了购物车图片,该图片默认占全屏预览。这里还可以进行滑动(左右滑动)查看其他图片。
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值