uni.chooseImage l临时路径IOS 退出软件之后失效异常

这段代码描述了一个功能,允许用户从相册中选择一张图片,进行压缩并保存到本地。图片路径随后被添加到背景列表和照片列表,并通过事件总线发送更新。此外,图片路径还被存储在本地存储中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

// 选择相册图片
tapPic() {
this.gData.openMedia = true;
uni.chooseImage({
count: 1, // 图片数量
sizeType:[‘compressed’], //压缩图
sourceType: [‘album’], //从相册选择
success: (res) => {
console.log(res)

					if (res.tempFilePaths.length > 0) {
						let path = res.tempFilePaths[0];
						
						uni.saveFile({
						      tempFilePath: path,
						      success: (res)  =>{
						        var savedFilePath = res.savedFilePath;
										 //检查文件是否已存在
										plus.io.resolveLocalFileSystemURL(savedFilePath,(entry)=> {
												console.log(entry.fullPath)
												try{
												let photoPath = "file://"+entry.fullPath;
												console.log(photoPath)
												for(let bgitem of this.bgList) {
													bgitem.checked = false;
												}
												this.bgList.push({
													url: photoPath,
													value: this.bgList[this.bgList.length - 1].value + 2,
													checked: true
												})
												this.photoList.push({
													url: photoPath,
													value: this.bgList[this.bgList.length - 1].value + 2,
													checked: false
												})
												this.$storage.editSet(this.backKey, photoPath);
												uni.setStorageSync(this.key, JSON.stringify(this.photoList));
												uni.$emit("editChatBack");
												}catch(e){
													console.log(e)
													//TODO handle the exception
												}
										
												//如果文件存在
										}, function (e) {
										});
						      }
						    });
					}
				}
			});
		},
在使用uni.chooseImage方法选择图片后,可以通过获取到的临时文件路径(tempFilePaths)来进行图片上传。需要注意的是,由于uni-app是基于H5的跨平台开发框架,因此不同平台的文件路径是不同的,需要进行兼容处理。 可以使用uni.uploadFile方法进行图片上传,示例代码如下: ```javascript uni.chooseImage({ success: function (res) { var tempFilePaths = res.tempFilePaths; uni.uploadFile({ url: 'http://example.com/upload', // 上传图片的接口地址 filePath: tempFilePaths[0], // 需要上传的图片临时文件路径 name: 'file', // 文件对应的 key,开发者在服务器端通过这个 key 可以获取到文件二进制内容 success: function (uploadRes) { console.log('上传成功', uploadRes); }, fail: function (err) { console.log('上传失败', err); } }); } }); ``` 需要注意的是,uni.chooseImage方法可能会返回多张图片的临时文件路径,需要根据实际需求进行处理。如果需要同时上传多张图片,可以使用Promise.all方法进行并行上传,示例代码如下: ```javascript uni.chooseImage({ count: 2, // 最多可以选择的图片张数,默认9 sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有 sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有 success: function (res) { var tempFilePaths = res.tempFilePaths; var promiseArr = []; for (var i = 0; i < tempFilePaths.length; i++) { promiseArr.push(uploadImage(tempFilePaths[i])); } Promise.all(promiseArr).then(function (result) { console.log('上传成功', result); }).catch(function (error) { console.log('上传失败', error); }); } }); function uploadImage(filePath) { return new Promise(function (resolve, reject) { uni.uploadFile({ url: 'http://example.com/upload', // 上传图片的接口地址 filePath: filePath, // 需要上传的图片临时文件路径 name: 'file', // 文件对应的 key,开发者在服务器端通过这个 key 可以获取到文件二进制内容 success: function (uploadRes) { resolve(uploadRes); }, fail: function (err) { reject(err); } }); }); } ``` 这样就可以同时上传多张图片,并在所有图片上传完成后进行统一处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值