gpt4 book ai didi

ios - UIImagePickerController didFinishPickingImage 返回不正确的 imageOrientation

转载 作者:可可西里 更新时间:2023-10-31 23:44:05 25 4
gpt4 key购买 nike

我正在使用 UImagePickerController 从相机捕获图像。

didFinishPickingImage 委托(delegate)方法中,我得到的图像的 imageOrientation 不正确。

举个例子,当我在肖像模式下拍照时,我得到了正确的图像,但是 imageOrientation = RIGHT (rawValue = 3) 而不是 imageOrientation = UP .

当我在横向模式下拍照时,我得到了正确的图像,但是 imageOrientation = DOWN 还是不正确。

正如您从代码中看到的那样,我在从选择器中获取图像后立即检查图像的方向。

override func viewDidLoad() 
{
picker.sourceType = UIImagePickerControllerSourceType.Camera
presentViewController(self.picker, animated: true, completion: nil)
}

func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!)
{
print("orientation: \(image.imageOrientation.rawValue)")
}

与其他类似问题相反,我得到了正确的图像,但 imageOrientation 总是不正确。

最佳答案

我也遇到了同样的问题。使用这个函数来解决:

func fixImageOrientation(src:UIImage)->UIImage {

if src.imageOrientation == UIImageOrientation.Up {
return src
}

var transform: CGAffineTransform = CGAffineTransformIdentity

switch src.imageOrientation {
case UIImageOrientation.Down, UIImageOrientation.DownMirrored:
transform = CGAffineTransformTranslate(transform, src.size.width, src.size.height)
transform = CGAffineTransformRotate(transform, CGFloat(M_PI))
break
case UIImageOrientation.Left, UIImageOrientation.LeftMirrored:
transform = CGAffineTransformTranslate(transform, src.size.width, 0)
transform = CGAffineTransformRotate(transform, CGFloat(M_PI_2))
break
case UIImageOrientation.Right, UIImageOrientation.RightMirrored:
transform = CGAffineTransformTranslate(transform, 0, src.size.height)
transform = CGAffineTransformRotate(transform, CGFloat(-M_PI_2))
break
case UIImageOrientation.Up, UIImageOrientation.UpMirrored:
break
}

switch src.imageOrientation {
case UIImageOrientation.UpMirrored, UIImageOrientation.DownMirrored:
CGAffineTransformTranslate(transform, src.size.width, 0)
CGAffineTransformScale(transform, -1, 1)
break
case UIImageOrientation.LeftMirrored, UIImageOrientation.RightMirrored:
CGAffineTransformTranslate(transform, src.size.height, 0)
CGAffineTransformScale(transform, -1, 1)
case UIImageOrientation.Up, UIImageOrientation.Down, UIImageOrientation.Left, UIImageOrientation.Right:
break
}

let ctx:CGContextRef = CGBitmapContextCreate(nil, Int(src.size.width), Int(src.size.height), CGImageGetBitsPerComponent(src.CGImage), 0, CGImageGetColorSpace(src.CGImage), CGImageAlphaInfo.PremultipliedLast.rawValue)!

CGContextConcatCTM(ctx, transform)

switch src.imageOrientation {
case UIImageOrientation.Left, UIImageOrientation.LeftMirrored, UIImageOrientation.Right, UIImageOrientation.RightMirrored:
CGContextDrawImage(ctx, CGRectMake(0, 0, src.size.height, src.size.width), src.CGImage)
break
default:
CGContextDrawImage(ctx, CGRectMake(0, 0, src.size.width, src.size.height), src.CGImage)
break
}

let cgimg:CGImageRef = CGBitmapContextCreateImage(ctx)!
let img:UIImage = UIImage(CGImage: cgimg)

return img
}

关于ios - UIImagePickerController didFinishPickingImage 返回不正确的 imageOrientation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36398786/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com