#独家
uniapp写了一个把图片转成canvas来实现固定尺寸的方法报错?

2023-07-28 0 970
export const cropImage = (filePath:string): Promise<string> => {
  return new Promise((resolve, reject) => {
    uni.getImageInfo({
      src: filePath,
      success(res) {
        let sx, sy, sw, sh, imgRatio, canvasRatio;
        const ctx = uni.createCanvasContext('canvas', this);
        canvasRatio = 2/3
        imgRatio = res.width / res.height
        if(imgRatio <= canvasRatio){
          sw = 1200
          sh = sw / canvasRatio
          sx = 0
          sy = (res.height - sh) / 2
        } else{
          sh = 1800
          sw = sh * canvasRatio
          sx = (res.width - sw) / 2
          sy = 0
        }
        ctx.drawImage(res.path, sx, sy, sw, sh, 0, 0, 1200, 1800);
        // @ts-ignore
        ctx.draw(false, setTimeout(() => {
          uni.canvasToTempFilePath({
            canvasId: 'canvas',
            success: (res) => {
              resolve(res.tempFilePath)
            },
            fail: (error) => {
              console.log('error', error);
              reject('')
            }
          }, this)
        }, 500))
      },
      fail() {
        reject('')
      }
    })
  })
}

我的项目有个功能选择相册中的一张图片,给他转成固定的尺寸。写完之后发现uni.canvasToTempFilePath总是报这个错误,不知道是哪里出现了问题?

<script lang="ts" setup>
import { ref } from 'vue'
// @ts-ignore
import { useUser } from '@/stores/app'
import { cropImage } from '@/utils/index'

const user = useUser()
const imgs = ref<string []>([])
user.photo.forEach(async item => {
  imgs.value.push(await cropImage(item.path as string));
})

errMsg: “canvasToTempFilePath:fail fail canvas is empty”

export const cropImage = (filePath:string, context:any): Promise<string> => {
  return new Promise((resolve, reject) => {
    uni.getImageInfo({
      src: filePath,
      success(res) {
        let sx, sy, sw, sh, imgRatio, canvasRatio;
        const ctx = uni.createCanvasContext('canvas', context);
        canvasRatio = 2/3
        imgRatio = res.width / res.height
        if(imgRatio <= canvasRatio){
          sw = 1200
          sh = sw / canvasRatio
          sx = 0
          sy = (res.height - sh) / 2
        } else{
          sh = 1800
          sw = sh * canvasRatio
          sx = (res.width - sw) / 2
          sy = 0
        }
        ctx.drawImage(res.path, sx, sy, sw, sh, 0, 0, 1200, 1800);
        ctx.draw(false, setTimeout(() => {
          uni.canvasToTempFilePath({
            canvasId: 'canvas',
            success: (res) => {
              resolve(res.tempFilePath)
            },
            fail: (error) => {
              console.log('error', error);
              reject('')
            }
          }, context)
        }, 500))
      },
      fail() {
        reject('')
      }
    })
  })
}

let that = this;
uni.chooseImage({
    sourceType: ['album'],
    success (res) {
      cropImage(res.tempFiles[0].path, that)
    }
});
收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

1. JK下载官网所有资源来源于开发团队,加入会员即可下载使用!如有问题请联系右下角在线客服!
2. JK下载官方保障所有软件都通过人工亲测,为每位会员用户提供安全可靠的应用软件、游戏资源下载及程序开发服务。
3. JK开发团队针对会员诉求,历经多年拥有现今开发成果, 每款应用程序上线前都经过人工测试无误后提供安装使用,只为会员提供安全原创的应用。
4. PC/移动端应用下载后如遇安装使用问题请联系右下角在线客服或提交工单,一对一指导解决疑难。

JK软件下载官网 技术分享 uniapp写了一个把图片转成canvas来实现固定尺寸的方法报错? https://www.jkxiazai.com/2288.html

JK软件应用商店是经过官方安全认证,保障正版软件平台

相关资源

官方客服团队

为您解决烦忧 - 24小时在线 专业服务