nui-app上传图片讲图片转化成base64

<script>
	var sourceType = [
		['camera'], //拍照
		['album'], //相册
		['camera', 'album'] //拍照或相册
	]
	var sizeType = [
		['compressed'], //压缩
		['original'], //原图
		['compressed', 'original'] //压缩或原图
	]
	export default {
		data() {
			return {
			
				sourceTypeIndex: 2,
				sourceType: ['拍照', '相册', '拍照或相册'],
				sizeTypeIndex: 2,
				sizeType: ['压缩', '原图', '压缩或原图'],
				countIndex: 9,
				count: 9,
				imageLength: 9, //限制图片张数
				
			}
			
		}, 
	   
		
			onUnload() {
				    this.imageList = [],
					this.sourceTypeIndex = 2,
					this.sourceType = ['拍照', '相册', '拍照或相册'],
					this.sizeTypeIndex = 2,
					this.sizeType = ['压缩', '原图', '压缩或原图'],
					this.countIndex = 8;
			},
			methods: {
				
				
			
			//选择图片	       
			chooseImage: async function() {
				   uni.getStorage({
				       key: 'imgbase64',
				       success: (res)=> {
							var data = res.data;
							this.img=data.img;
				       }
				   });
			        
					uni.chooseImage({
						sourceType: sourceType[this.sourceTypeIndex],
						sizeType: sizeType[this.sizeTypeIndex],
						count:9,
						success: (res) => {
							var tempFilePaths = res.tempFilePaths;
							
							
						},
						
					})
					
					},
				
				  urlTobase64(url) {
              uni.request({
			                    url: url,
			                    method: 'GET',
			                    responseType: 'arraybuffer',
			                    success: res => {
			                        let base64 = uni.arrayBufferToBase64(res.data); //把arraybuffer转成base64
			                        base64 = 'data:image/jpeg;base64,' + base64; //不加上这串字符,在页面无法显示
									this.img=base64;
									console.log('urlTobase64',this.img);
			                    }
			                });
							
			          },	
				
	   }
	}
发布了50 篇原创文章 · 获赞 7 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/xh_960125/article/details/105139749