uniapp iOS微信分享要求分享图必须小于20k限制的解决

//分享给朋友
	wxManShare(){
				
		if(this.$platform=='android'){
			let iurl=this.liveInfo.limg
			uni.share({
			    provider: "weixin",
			    scene: "WXSceneSession",
			    type: 0,
			    href: `https://m.xxx.com/lives/room/${this.roomid}`,
			    title: this.liveInfo.ltitle,
			    summary: descInfo,
			    imageUrl: iurl,
			    success: ()=>{},
			    fail: ()=>{}
			});
			this.$refs.sharePopup.close()
		}else{
			let iurl = "https://img-12xxx8.file.myqcloud.com/other/logo/live_app.png"  //默认准备一张备用分享图
			let that = this  //需要改变指向
			uni.downloadFile({  //网上图片资源转为本地
				url: that.liveInfo.limg,
				success: (res) => {
					if (res.statusCode == 200) {
						let imgType=that.liveInfo.limg.split(".").pop()  //获取图片类型,只有jpg类型图片才支持图片质量quality参数
						uni.compressImage({  //压缩图片
							src: res.tempFilePath,
							quality: 20,
							width: imgType=="jpg"?"200px":"120px",
							height: "auto",
							success: ret => {
								uni.getFileInfo({  //获取图片信息
									filePath: ret.tempFilePath,
									success: imgInfo => {
										if(imgInfo.size/1024<20){  //判断图片大小,小于20k才有效
											iurl = ret.tempFilePath
										}
									},
									complete: () => {
										uni.share({
										    provider: "weixin",
										    scene: "WXSceneSession",
										    type: 0,
										    href: `https://m.kxxxe.com/lives/room/${that.roomid}`,
										    title: that.liveInfo.ltitle,
										    summary: descInfo,
										    imageUrl: iurl,
										    success: ()=>{},
										    fail: ()=>{}
										});
										that.$refs.sharePopup.close()
									} 
								})
							},
							fail: () => {
								uni.share({
								    provider: "weixin",
								    scene: "WXSceneSession",
								    type: 0,
								    href: `https://m.kxxxe.com/lives/room/${that.roomid}`,
								    title: that.liveInfo.ltitle,
								    summary: descInfo,
								    imageUrl: iurl,
								    success: ()=>{},
								    fail: ()=>{}
								});
								that.$refs.sharePopup.close()
							}
						})
					}
				},
				fail: ()=>{
					uni.share({
					    provider: "weixin",
					    scene: "WXSceneSession",
					    type: 0,
					    href: `https://m.kxxx.com/lives/room/${that.roomid}`,
					    title: that.liveInfo.ltitle,
					    summary: descInfo,
					    imageUrl: iurl,
					    success: ()=>{},
					    fail: ()=>{}
					});
					that.$refs.sharePopup.close()
				}
			});
		}
	},

Tips

也可采取另一种思路,先获取图片文件大小,根据图片大小区间使用不同的压缩比例。
posted @ 2022-12-28 17:15  huihuihero  阅读(806)  评论(0编辑  收藏  举报