uni app预览static目录下图片

uni app预览图片,,可以选择static目录下图片

var index = 0;
uni.previewImage({
	urls: ['../../../../static/img1.jpg', '../../../../static/img2.jpg'],
	current: index,
	longPressActions: {
		itemList: ['发送给朋友', '保存图片', '收藏'],
		success: function(data) {
			console.log('选中了第' + (data.tapIndex + 1) + '个按钮,第' + (data.index + 1) + '张图片');
		},
		fail: function(err) {
			console.log(err.errMsg);
		}
	}
});

参考
https://uniapp.dcloud.io/api/media/image?id=unipreviewimageobject

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在uni-app中实现图片双指或双击放大缩小的功能,你可以使用uni-app提供的`uni.createSelectorQuery()`和`canvas` API来实现。 具体实现步骤如下: 1. 在`<template>`标签中放置一个`<canvas>`标签,用于显示图片; 2. 在`<script>`标签中,使用`uni.createSelectorQuery()`方法获取`<canvas>`标签的上下文`canvasContext`; 3. 使用`canvasContext.drawImage()`方法绘制图片; 4. 监听`<canvas>`标签的`touchstart`、`touchmove`、`touchend`事件,分别处理双指缩放和双击放大的逻辑; 5. 在事件处理函数中,使用`canvasContext.scale()`方法实现双指缩放,使用`canvasContext.translate()`和`canvasContext.scale()`方法实现双击放大。 以下是示例代码: ``` <template> <canvas style="width:100%;height:100%;" @touchstart="touchStart" @touchmove="touchMove" @touchend="touchEnd"></canvas> </template> <script> export default { data() { return { canvasContext: null, // canvas上下文 imgWidth: 0, // 图片宽度 imgHeight: 0, // 图片高度 imgScale: 1, // 图片缩放比例 lastTouchDistance: 0, // 上一次双指触摸距离 lastTouchTime: 0, // 上一次触摸时间 lastTouchX: 0, // 上一次触摸x坐标 lastTouchY: 0 // 上一次触摸y坐标 }; }, mounted() { // 获取canvas上下文 uni.createSelectorQuery().select('canvas').fields({ node: true, size: true }).exec((res) => { this.canvasContext = res[0].node.getContext('2d'); }); // 加载图片 let img = new Image(); img.src = '图片地址'; img.onload = () => { // 计算图片宽高和缩放比例 this.imgWidth = img.width; this.imgHeight = img.height; this.imgScale = Math.min(uni.upx2px(750) / this.imgWidth, uni.upx2px(750) / this.imgHeight); // 绘制图片 this.canvasContext.drawImage(img, 0, 0, this.imgWidth * this.imgScale, this.imgHeight * this.imgScale); }; }, methods: { touchStart(e) { if (e.touches.length === 2) { // 双指触摸,记录初始触摸距离 this.lastTouchDistance = Math.sqrt(Math.pow(e.touches[1].pageX - e.touches[0].pageX, 2) + Math.pow(e.touches[1].pageY - e.touches[0].pageY, 2)); } else if (e.touches.length === 1) { // 单指触摸,记录触摸时间和坐标 let currentTime = new Date().getTime(); if (currentTime - this.lastTouchTime < 300 && Math.abs(e.touches[0].pageX - this.lastTouchX) < 30 && Math.abs(e.touches[0].pageY - this.lastTouchY) < 30) { // 双击触摸,放大图片 this.canvasContext.translate((e.touches[0].pageX - this.imgWidth * this.imgScale / 2) * (1 - 1.5), (e.touches[0].pageY - this.imgHeight * this.imgScale / 2) * (1 - 1.5)); this.canvasContext.scale(1.5, 1.5); this.imgScale *= 1.5; this.lastTouchTime = 0; } else { // 单击触摸,记录触摸时间和坐标 this.lastTouchTime = currentTime; this.lastTouchX = e.touches[0].pageX; this.lastTouchY = e.touches[0].pageY; } } }, touchMove(e) { if (e.touches.length === 2) { // 双指触摸,计算触摸距离差值 let touchDistance = Math.sqrt(Math.pow(e.touches[1].pageX - e.touches[0].pageX, 2) + Math.pow(e.touches[1].pageY - e.touches[0].pageY, 2)); let distanceDelta = touchDistance - this.lastTouchDistance; if (distanceDelta !== 0) { // 缩放图片 let scaleDelta = distanceDelta / uni.upx2px(750) * 2; this.canvasContext.translate((e.touches[0].pageX + e.touches[1].pageX) / 2 - this.imgWidth * this.imgScale / 2, (e.touches[0].pageY + e.touches[1].pageY) / 2 - this.imgHeight * this.imgScale / 2); this.canvasContext.scale(1 + scaleDelta, 1 + scaleDelta); this.canvasContext.translate(this.imgWidth * this.imgScale / 2 - (e.touches[0].pageX + e.touches[1].pageX) / 2, this.imgHeight * this.imgScale / 2 - (e.touches[0].pageY + e.touches[1].pageY) / 2); this.imgScale *= 1 + scaleDelta; this.lastTouchDistance = touchDistance; } } }, touchEnd(e) { this.lastTouchDistance = 0; } } }; </script> ``` 注意,以上代码仅供参考,具体实现还需要根据实际情况进行调整。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值