uni-app(六)生成海报图片路径问题

  插件市场,搜索 painter 插件,这款插件很是强大,初次使用,存在一个问题,那就是由于跨域导致的图片路径问题

  • 如果是存储在服务器的图片,需要使用 downloadFile 获取图片路径,跳过跨域机制
    let that = this
    uni.downloadFile({
       url: that.img,
       success: function(res) {
           that.downloadPoster = res.tempFilePath
       },
       fail: function() {
           console.log('fail')
       }
    })

     

  • 下一步,需要用 getImageInfo 获取图片信息,输出可以直接使用的路径
    // 封装图片信息获取方法 
    promisify: promise => {
       return (options, ...params) => {
           return new Promise((resolve, reject) => {
              const extras = {
                  success: resolve,
                  fail: reject
              }
              promise({ ...options, ...extras }, ...params)
           })
        }
    }
    // 输出路径
    getPoster() {
        let that = this
        const getImageInfo = that.promisify(uni.getImageInfo)
        Promise.all([
           getImageInfo({
              src: that.downloadPoster
           })
        ]).then(res => {
           // 最终路径
           let path= res[0].path
           console.log(path)
        })
    }

     

 

posted @ 2021-03-17 17:51  一杯龙井解千愁  阅读(444)  评论(0编辑  收藏  举报