导航

    Taro 社区

    Taro

    • 注册
    • 登录
    • 搜索
    • 版块
    • 最新
    • 话题
    • 热门
    • 群组
    1. 主页
    2. 1013156-34430897
    1
    • 资料
    • 关注
    • 粉丝
    • 主题
    • 帖子
    • 最佳
    • 群组

    1013156-34430897

    @1013156-34430897

    0
    声望
    2
    帖子
    20
    资料浏览
    0
    粉丝
    0
    关注
    注册时间 最后登录

    1013156-34430897 关注

    1013156-34430897 发布的帖子

    • 小程序选择图片直接转base64点一个方法。

      新的微信里出现了
      【FileSystemManager.readFile(Object object)】方法

      可以直接指定读取文件的编码格式。

      那么filePath使用chooseImage 获得的文件零时路径 不就可以获得 base64 的图片文件了么?

      试了一下 以下代码可行

      insertImage(event) {
      		console.log('InsertImg', event);
      		Taro.chooseImage({
      			count: 1,
      			sizeType: [ 'original', 'compressed' ],
      			sourceType: [ 'album', 'camera' ]
      		})
      			.then(async (res) => {
      				let file = await fileToBase64(res.tempFilePaths[0]);
      				console.log(file);
      				if (file) {
      					this.EditorContext.insertImage({ src: `data:image.jpeg;base64,${file}` });
      				}
      			})
      			.catch((err) => {
      				console.error(err);
      			});
      		// this.EditorContext.insertImage();
      	}
      
      
      export const fileToBase64 = (filePath) => {
      	return new Promise((resolve, reject) => {
      		let fileManager = Taro.getFileSystemManager();
      		fileManager.readFile({
      			filePath,
      			encoding: 'base64',
      			success: (e: any) => {
      				resolve(e.data);
      			}
      		});
      	});
      };
      
      
      发布在 微信小程序
      1
      1013156-34430897
    • RE: Taro中可以使用Loadash吗

      人肉写 ?

      /**
       * @description: 函数防抖 当持续触发事件时,一定时间段内没有再触发事件,
       * 事件处理函数才会执行一次,如果设定的时间到来之前,又一次触发了事件,就重新开始延时。
       * 使用:注意!不能使用箭头 函数
       * @param {Function} fn 执行的函数
       * @param {number} interval 延迟时间
       * @return: function
       */
      export const debounce = function(fn: Function, interval: number = 1000, self?) {
       let timer: any; // 计时器
           return function() {
               clearTimeout(timer); // 清空上次未完成的计时器
      	 let _this = self || this;
      	 timer = setTimeout(() => {
      	 fn.call(_this, arguments); 
      	 }, interval);
           };
      };
      
      
      发布在 Taro
      1
      1013156-34430897