【uniapp】微信小程序 , 海报轮播图弹窗,点击海报保存到本地,长按海报图片分享,收藏或保存,showShareImageMenu ,saveImageToPhotosAlbum

在这里插入图片描述
uivew 2.0
uniapp 海报画板 DCloud 插件市场

第一步,下载插件并导入HbuilderX

第二步,文件内 引入 海报组件

<template>
	<painter ref="haibaorefs"></painter>
<template>
<script>
	import painter from '@/components/painter.vue'
	export default {
	components: {painter},
	methods: {
	// 点击打开海报弹窗
		clickFun(){
			this.$refs.haibaorefs.open();
		}
	}
}
<script>

第三步,子组件 海报 源码, components/painter.vue 文件内容

<template>
	<!-- 弹窗 -->
	<u-popup :show="goodsshow" mode="center" round='26rpx' z-index='10076' bgColor='transparent' @close="goodsclose">
		<view class="popupbox">
			<view class="swiperbox">
				<swiper 
				class="swiper" 
				:indicator-dots="false" 
				:autoplay="false" 
				:circular="true" 
				skip-hidden-item-layout
				previous-margin='35rpx'
				next-margin='35rpx'
				@change="swiperChange">
					<swiper-item v-for="(item,i) in list" :key="i">
						<view class="" v-if="item.coverimg">
							<image :src="item.coverimg" mode="" class="swiperImg"  show-menu-by-longpress></image>
						</view>
						
						<l-painter
							v-else
							@success='successFun($event,i)'
							isCanvasToTempFilePath
							path-type="url"
							css="width: 600rpx;height:900rpx; box-sizing: border-box;  position: relative;">
							<l-painter-image :src="item.image"
							css="width: 600rpx;height:900rpx;border-radius: 20rpx;object-fit: cover;"/>
								<l-painter-view css="position: absolute; bottom: 40rpx; right: 40rpx; background: #fff;  padding:10rpx 8rpx;border-radius: 10rpx; ">
									<l-painter-image :src="erweima"
									css="width: 146rpx;height:143rpx;object-fit: cover;"/>
								</l-painter-view>
						</l-painter>
				
					</swiper-item>
				</swiper>
			</view>
			<view class="btnbox" @click="saveFun()">
				<!-- <view class="btn btn1" @click="goodsshow = false">长按识别分享海报</view> -->
				<view class="btn btn2">保存到本地</view>
			</view>

		</view>
	</u-popup>
</template>

<script>
	export default {
		components: {},
		props: {
			// list: {
			//   type: Array,
			//   default: null
			// }
		},
		data() {
			return {
				goodsshow: false,
				current: 0,
				list: [{
						image: 'https://m.360buyimg.com/babel/jfs/t1/196317/32/13733/288158/60f4ea39E6fb378ed/d69205b1a8ed3c97.jpg',
						coverimg:'', // 海报生成图片的临时地址
						id: 11
					},
					{
						image: 'https://m.360buyimg.com/babel/jfs/t1/196317/32/13733/288158/60f4ea39E6fb378ed/d69205b1a8ed3c97.jpg',
						coverimg:'',
						id: 11
					},
					{
						image: require('@/static/img/1.jpg'),
						coverimg:'',
						id: 11
					},
				],
				erweima: require('@/static/img/1.jpg')
			}
		},
		mounted() {
		
		},
		methods: {
			open(){
				this.goodsshow = true;
			},
			goodsclose() {
				this.goodsshow = false;
			},
			swiperChange(e) {
				this.current = e.detail.current;
			},
			// 接收海报临时路径
			successFun(e,i){
				console.log('接收海报临时路径',e,i)
				uni.getSavedFileList({
				  success: function (res) {
				    console.log(res.fileList);
				  }
				});
				
				this.list.forEach((item,index) => {
						if(index == i){
							item.coverimg = e;
						}
				});
			},
			
			// 保存到本地
			saveFun(){
				let that = this;
				console.log(that.list[that.current].coverimg,'保存图片的临时路径和下标',this.current)
					uni.saveImageToPhotosAlbum({
						filePath: that.list[that.current].coverimg,
						success: function () {
							console.log('save success');
							uni.$u.toast('海报已保存到相册')
						}
					});
			},
			// 分享给好友(底部弹出)
			shareFun() {
				let that = this;
				console.log(that.list[that.current].coverimg, '保存图片的临时路径')
				uni.showShareImageMenu({
				path: that.list[that.current].coverimg,
				success: function () {
					console.log('save success');
				},
				complete:function(resres){
				console.log(resres,'不论成功失败都显示')
				 	}
				 });
			},
			// 跳转
			navTo(url) {
				uni.navigateTo({
					url: url
				})
			},
		},
	}
</script>

<style lang='scss' scoped>
	swiper-item {
	/* 	display: flex;
		justify-content: center;
		align-items: center; */
	}

	.popupbox {
		display: flex;
		justify-content: center;
		align-items: center;
		flex-direction: column;

		.swiperbox {
			background-color: transparent;
			width: 750rpx;
			height: 900rpx;
			box-sizing: border-box;

			.swiper {
				width: 750rpx;
				height: 900rpx;
				overflow: hidden;
				border-radius: 20rpx;
				box-sizing: border-box;
			
				.swiperImg {
					width: 600rpx;
					height: 900rpx;
					border-radius: 20rpx;
				}
				
			}
		}

		.btnbox {
			margin: 50rpx auto 0;
			.btn {
				width: 315px;
				height: 96rpx;
				line-height: 96rpx;
				text-align: center;
				border-radius: 48rpx;
				font-size: 32rpx;
				font-weight: 500;
			}
			.btn2 {
				background: linear-gradient(177deg, #F2582F, #E1200B);
				color: #ffffff;
			}
		}


	}
</style>
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 可以使用 uni.saveImageToPhotosAlbum 接口将图片保存微信小程序的本地相册中。 首先,需要在小程序app.json 中添加如下权限: ``` "permission": { "scope.writePhotosAlbum": { "desc": "保存图片到相册需要使用" } } ``` 然后,在需要保存图片的页面或组件中,可以调用 uni.saveImageToPhotosAlbum 接口,如下所示: ``` uni.saveImageToPhotosAlbum({ filePath: '图片文件路径', success: function () { console.log('保存图片到本地相册成功'); }, fail: function () { console.log('保存图片到本地相册失败'); } }); ``` 注意: - filePath 参数为图片文件的本地路径。 - uni.saveImageToPhotosAlbum 接口需要用户授权,如果用户拒绝授权,则无法保存图片到本地相册。 在用户授权后,就可以将图片保存微信小程序的本地相册中了。 ### 回答2: 在uniapp中,可以使用uni.request和uni.downloadFile这两个api来保存微信小程序中的图片到本地。 首先,我们需要将图片保存到服务器上,可以通过uni.request来完成。将图片的url传递给uni.request,并设置responseType为arraybuffer。示例代码如下: uni.request({ url: '图片url', responseType: 'arraybuffer', success: res => { // res.data即为图片的二进制数据 // 接下来将图片保存到本地 } }) 接着,我们可以通过uni.downloadFile api来将图片保存到本地。将res.data作为参数传递给uni.downloadFile的url参数,并设置保存路径filePath。示例代码如下: uni.downloadFile({ url: 'data:image/png;base64,' + uni.arrayBufferToBase64(res.data), success: res => { if (res.statusCode === 200) { // res.tempFilePath为图片保存到本地的临时文件路径 console.log(res.tempFilePath) } } }) 最后,我们可以将保存到本地的临时文件移动到用户的相册中,使用uni.saveImageToPhotosAlbum来完成。示例代码如下: uni.saveImageToPhotosAlbum({ filePath: res.tempFilePath, success: res => { console.log('保存成功') }, fail: err => { console.log('保存失败') } }) 以上就是使用uniapp微信小程序保存图片到本地的简单步骤。需要注意的是,uniapp也提供了其他的api和方法来实现保存图片的功能,可以根据具体需求选择适合的方法。 ### 回答3: uniapp是一种跨平台开发框架,可以同时开发多个平台的应用程序,包括微信小程序。在uniapp中,我们可以使用uni-app插件来实现将图片保存到本地的功能。 具体步骤如下: 1. 首先,我们需要在uniapp项目中引入uni-app插件,可以通过在项目的根目录下的`uni.xml`文件中的`easycom`节点中添加`"@dcloudio/uni-save-image"`来引入插件。 2. 在需要保存图片的页面中,使用`uni.saveImageToPhotosAlbum()`方法来保存图片到本地相册。该方法需要传入一个对象参数,其中包含要保存图片路径`filePath`和一个回调函数`success`用来处理保存成功的情况,和一个`fail`函数用来处理保存失败的情况。 3. 在回调函数中,我们可以通过判断返回值中的`errMsg`属性来判断保存是否成功,如果成功则显示保存成功的提示,否则显示保存失败的提示。 下面是一个示例代码: ``` <template> <view> <image src="/static/image.png"></image> <button @click="saveImage">保存图片</button> </view> </template> <script> export default { methods: { saveImage() { uni.saveImageToPhotosAlbum({ filePath: '/static/image.png', success(res) { uni.showToast({ title: '保存成功', icon: 'success' }); }, fail(res) { uni.showToast({ title: '保存失败', icon: 'none' }); } }); } } } </script> ``` 以上就是在uniapp微信小程序保存图片到本地的简单步骤和示例代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值