记录一次失败的旋转

296 阅读1分钟
// 修复ios14版本以下的旋转问题
    getPhotoOrientation (context, img, imgW, imgH) {
      return new Promise((resolve, reject) => {
        const ua = navigator.userAgent.toLowerCase()
        const isIos = ua.match(/cpu iphone os (.*?) like mac os/)
        console.log('isIos:', isIos)
        if (!isIos) {
          console.log('noIos-yes:')
          context.drawImage(img, 0, 0, imgW, imgH)
          resolve({ code: 1 })
          return
        }
        const version = isIos[1]?.replace(/_/g, '.')
        console.log('version:', version)
        if (Number(version >= 14)) {
          console.log('版本号大于14:')
          context.drawImage(img, 0, 0, imgW, imgH)
          resolve({ code: 1 })
          return
        }
        EXIF.getData(img, function () {
          console.log('版本号小于14:')
          const orient = EXIF.getTag(this, 'Orientation') /** * 1 表示旋转0度,也就是没有旋转。   6 表示顺时针旋转90度 8 表示逆时针旋转90度  3 旋转180度 **/
          console.log('orient', orient)
          if (orient === 6 || orient === 3 || orient === 8) {
            switch (orient) {
              case 6: // 旋转90度
                context.width = imgH
                context.height = imgW
                context.rotate(Math.PI / 2)
                // (0,-imgH) 从旋转原理图那里获得的起始点
                context.drawImage(this, 0, -imgH, imgW, imgH)
                resolve({ code: 1 })
                break
              case 3: // 旋转180度
                context.rotate(Math.PI)
                context.drawImage(this, -imgW, -imgH, imgW, imgH)
                resolve({ code: 1 })
                break
              case 8: // 旋转-90度
                context.width = imgH
                context.height = imgW
                context.rotate(3 * Math.PI / 2)
                context.drawImage(this, -imgW, 0, imgW, imgH)
                resolve({ code: 1 })
                break
            }
            return
          }
          context.drawImage(img, 0, 0, imgW, imgH)
          resolve({ code: 1 })
        })
      })
    },