-
Notifications
You must be signed in to change notification settings - Fork 145
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
在ios上图片旋转 #11
Comments
我也遇到这个问题,谁能来解决下 啊~~~~~~~~~ |
为什么裁剪出来的图片会旋转了90度 |
EXIF中的Orientation代表照片方向,有1—8八个值,ios竖着拍照Orientation=6,所以照片歪着,需要调整 |
写了个压缩/旋转类,可以参考下 import EXIF from 'exif-js'
class ImageFile {
dataURLtoBlob (dataURL = '') {
let arr = dataURL.split(',')
let mime = arr[0].match(/:(.*?);/)[1]
let bstr = window.atob(arr[1])
let n = bstr.length
let u8arr = new Uint8Array(n)
while (n--) {
u8arr[n] = bstr.charCodeAt(n)
}
return new window.Blob([u8arr], {type: mime})
}
orientation (file = null) {
return new Promise((resolve, reject) => {
EXIF.getData(file, function () {
const orientation = EXIF.getTag(this, 'Orientation')
resolve(orientation)
})
})
}
toDataURL (file = null) {
const fr = new window.FileReader()
fr.readAsDataURL(file)
return new Promise((resolve, reject) => {
fr.onload = (f) => {
resolve(f.target.result)
}
})
}
compress ({file = null, maxWidth = 1000, maxHeight = 1000, ratio = 1}) {
const self = this
let img = document.createElement('img')
let canvas = document.createElement('canvas')
const ctx = canvas.getContext('2d')
self.toDataURL(file).then(base64 => {
img.src = base64
})
return new Promise((resolve, reject) => {
img.onload = async () => {
const orientation = await self.orientation(file)
const originWidth = img.width
const originHeight = img.height
let targetWidth = originWidth
let targetHeight = originHeight
if (originWidth > maxWidth || originHeight > maxHeight) {
if (originWidth / originHeight > maxWidth / maxHeight) {
targetWidth = maxWidth
targetHeight = Math.round(maxWidth * (originHeight / originWidth))
} else {
targetHeight = maxHeight
targetWidth = Math.round(maxHeight * (originWidth / originHeight))
}
}
canvas.width = targetWidth
canvas.height = targetHeight
if (orientation && orientation !== 1) {
switch (orientation) {
case 6:
canvas.width = targetHeight
canvas.height = targetWidth
ctx.rotate(Math.PI / 2)
ctx.drawImage(img, 0, -targetHeight, targetWidth, targetHeight)
break
case 3:
ctx.rotate(Math.PI)
ctx.drawImage(img, -targetWidth, -targetHeight, targetWidth, targetHeight)
break
case 8:
canvas.width = targetHeight
canvas.height = targetWidth
ctx.rotate(3 * Math.PI / 2)
ctx.drawImage(img, -targetWidth, 0, targetWidth, targetHeight)
break
}
} else {
ctx.drawImage(img, 0, 0, targetWidth, targetHeight)
}
let dataURL = canvas.toDataURL(file.type, ratio)
let blob = await self.dataURLtoBlob(dataURL)
img = null
canvas = null // 释放内存
resolve({dataURL, blob})
}
})
}
}
export default ImageFile |
重置图片方法 做一个转换
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
如果我使用拍照获得的图片作为image_src,预览的时候是正常的,但是裁剪出来的图片旋转了90度。
The text was updated successfully, but these errors were encountered: