Swift 从UIImagePickerController获取图像后,UIImageView会旋转iPhone 5的图像
我正在使用UIImagePickerController使用照相机捕获图像。我的应用程序支持的方向是纵向。我看到iPhone5有奇怪的行为。我使用的是Xcode 7和swift 2.0。iPhone5OS版本是8.4。我的应用程序的部署目标是8.0 问题是。 1.对于iPhone5,在捕获图像后,图像将以捕获图像的相应模式显示。但在我按下“使用照片”标准选项后,当图像显示在UIImageView上时,图像会自动向左旋转。不知道为什么。若我从照片库中选择“图像”,则图像不会旋转。我不希望图像被旋转。 我看过类似的文章,有更好的解释和真实的图像,但没有得到回答。 我尝试了这篇文章中几乎所有的快速解决方案:还有其他的文章,但似乎没有任何效果。我使用了shouldAutorotate()、sFunc_imageFixOrientation()和从本文添加扩展名Swift 从UIImagePickerController获取图像后,UIImageView会旋转iPhone 5的图像,swift,uiimagepickercontroller,uiimageorientation,Swift,Uiimagepickercontroller,Uiimageorientation,我正在使用UIImagePickerController使用照相机捕获图像。我的应用程序支持的方向是纵向。我看到iPhone5有奇怪的行为。我使用的是Xcode 7和swift 2.0。iPhone5OS版本是8.4。我的应用程序的部署目标是8.0 问题是。 1.对于iPhone5,在捕获图像后,图像将以捕获图像的相应模式显示。但在我按下“使用照片”标准选项后,当图像显示在UIImageView上时,图像会自动向左旋转。不知道为什么。若我从照片库中选择“图像”,则图像不会旋转。我不希望图像被旋转
这就是我观察到的。当您使用相机拍摄图像时,图像将逆时针旋转90度,图像方向设置为UIImageOrientation。因此,函数sFunc_imageFixOrientation中的代码将不起作用。如果您从照片库中选择图像,则图像将按原样显示 修改后的函数:
func sFunc_imageFixOrientation(img:UIImage) -> UIImage {
// We need to calculate the proper transformation to make the image upright.
// We do it in 2 steps: Rotate if Left/Right/Down, and then flip if Mirrored.
var transform:CGAffineTransform = CGAffineTransformIdentity
// Rotate image clockwise by 90 degree
if (img.imageOrientation == UIImageOrientation.Up) {
transform = CGAffineTransformTranslate(transform, 0, img.size.height);
transform = CGAffineTransformRotate(transform, CGFloat(-M_PI_2));
}
if (img.imageOrientation == UIImageOrientation.Down
|| img.imageOrientation == UIImageOrientation.DownMirrored) {
transform = CGAffineTransformTranslate(transform, img.size.width, img.size.height)
transform = CGAffineTransformRotate(transform, CGFloat(M_PI))
}
if (img.imageOrientation == UIImageOrientation.Left
|| img.imageOrientation == UIImageOrientation.LeftMirrored) {
transform = CGAffineTransformTranslate(transform, img.size.width, 0)
transform = CGAffineTransformRotate(transform, CGFloat(M_PI_2))
}
if (img.imageOrientation == UIImageOrientation.Right
|| img.imageOrientation == UIImageOrientation.RightMirrored) {
transform = CGAffineTransformTranslate(transform, 0, img.size.height);
transform = CGAffineTransformRotate(transform, CGFloat(-M_PI_2));
}
if (img.imageOrientation == UIImageOrientation.UpMirrored
|| img.imageOrientation == UIImageOrientation.DownMirrored) {
transform = CGAffineTransformTranslate(transform, img.size.width, 0)
transform = CGAffineTransformScale(transform, -1, 1)
}
if (img.imageOrientation == UIImageOrientation.LeftMirrored
|| img.imageOrientation == UIImageOrientation.RightMirrored) {
transform = CGAffineTransformTranslate(transform, img.size.height, 0);
transform = CGAffineTransformScale(transform, -1, 1);
}
// Now we draw the underlying CGImage into a new context, applying the transform
// calculated above.
let ctx:CGContextRef = CGBitmapContextCreate(nil, Int(img.size.width), Int(img.size.height),
CGImageGetBitsPerComponent(img.CGImage), 0,
CGImageGetColorSpace(img.CGImage),
CGImageGetBitmapInfo(img.CGImage).rawValue)!;
CGContextConcatCTM(ctx, transform)
if (img.imageOrientation == UIImageOrientation.Left
|| img.imageOrientation == UIImageOrientation.LeftMirrored
|| img.imageOrientation == UIImageOrientation.Right
|| img.imageOrientation == UIImageOrientation.RightMirrored
|| img.imageOrientation == UIImageOrientation.Up
) {
CGContextDrawImage(ctx, CGRectMake(0,0,img.size.height,img.size.width), img.CGImage)
} else {
CGContextDrawImage(ctx, CGRectMake(0,0,img.size.width,img.size.height), img.CGImage)
}
// And now we just create a new UIImage from the drawing context
let cgimg:CGImageRef = CGBitmapContextCreateImage(ctx)!
let imgEnd:UIImage = UIImage(CGImage: cgimg)
return imgEnd
}
}
只有在从相机拍摄图像时才调用此函数。我知道我的答案并不完美,但如果没有任何结果,您肯定可以尝试
func sFunc_imageFixOrientation(img:UIImage) -> UIImage {
// We need to calculate the proper transformation to make the image upright.
// We do it in 2 steps: Rotate if Left/Right/Down, and then flip if Mirrored.
var transform:CGAffineTransform = CGAffineTransformIdentity
// Rotate image clockwise by 90 degree
if (img.imageOrientation == UIImageOrientation.Up) {
transform = CGAffineTransformTranslate(transform, 0, img.size.height);
transform = CGAffineTransformRotate(transform, CGFloat(-M_PI_2));
}
if (img.imageOrientation == UIImageOrientation.Down
|| img.imageOrientation == UIImageOrientation.DownMirrored) {
transform = CGAffineTransformTranslate(transform, img.size.width, img.size.height)
transform = CGAffineTransformRotate(transform, CGFloat(M_PI))
}
if (img.imageOrientation == UIImageOrientation.Left
|| img.imageOrientation == UIImageOrientation.LeftMirrored) {
transform = CGAffineTransformTranslate(transform, img.size.width, 0)
transform = CGAffineTransformRotate(transform, CGFloat(M_PI_2))
}
if (img.imageOrientation == UIImageOrientation.Right
|| img.imageOrientation == UIImageOrientation.RightMirrored) {
transform = CGAffineTransformTranslate(transform, 0, img.size.height);
transform = CGAffineTransformRotate(transform, CGFloat(-M_PI_2));
}
if (img.imageOrientation == UIImageOrientation.UpMirrored
|| img.imageOrientation == UIImageOrientation.DownMirrored) {
transform = CGAffineTransformTranslate(transform, img.size.width, 0)
transform = CGAffineTransformScale(transform, -1, 1)
}
if (img.imageOrientation == UIImageOrientation.LeftMirrored
|| img.imageOrientation == UIImageOrientation.RightMirrored) {
transform = CGAffineTransformTranslate(transform, img.size.height, 0);
transform = CGAffineTransformScale(transform, -1, 1);
}
// Now we draw the underlying CGImage into a new context, applying the transform
// calculated above.
let ctx:CGContextRef = CGBitmapContextCreate(nil, Int(img.size.width), Int(img.size.height),
CGImageGetBitsPerComponent(img.CGImage), 0,
CGImageGetColorSpace(img.CGImage),
CGImageGetBitmapInfo(img.CGImage).rawValue)!;
CGContextConcatCTM(ctx, transform)
if (img.imageOrientation == UIImageOrientation.Left
|| img.imageOrientation == UIImageOrientation.LeftMirrored
|| img.imageOrientation == UIImageOrientation.Right
|| img.imageOrientation == UIImageOrientation.RightMirrored
|| img.imageOrientation == UIImageOrientation.Up
) {
CGContextDrawImage(ctx, CGRectMake(0,0,img.size.height,img.size.width), img.CGImage)
} else {
CGContextDrawImage(ctx, CGRectMake(0,0,img.size.width,img.size.height), img.CGImage)
}
// And now we just create a new UIImage from the drawing context
let cgimg:CGImageRef = CGBitmapContextCreateImage(ctx)!
let imgEnd:UIImage = UIImage(CGImage: cgimg)
return imgEnd
}
}