UIImagePickerController didFinishPickingImage返回错误的imageOrientation

时间:2016-04-04 09:21:50

标签: ios swift camera uiimagepickercontroller

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

didFinishPickingImage委托方法中,我收到的图片不正确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始终不正确。

1 个答案:

答案 0 :(得分:0)

我也遇到了同样的问题。使用此功能解决:

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
}