ImageView在使用imagePicker时旋转

我有一个表视图,在单元格中,有一个图像和2个标签。 我正在使用imagePicker从我的相机胶卷中选择一张照片并将该照片放入单元格图像中。 但是,当照片是肖像模式照片时,图像旋转90度。

在下面的代码中,发布是一个连接到表视图单元格中的imageview的sockets,我初始化了一个UIImagePickerController

 class MakePostVC: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate { @IBOutlet weak var postImg: UIImageView! @IBOutlet weak var postTitle: UITextField! @IBOutlet weak var postDescrip: UITextField! var imgPicker: UIImagePickerController! override func viewDidLoad() { super.viewDidLoad() postImg.layer.cornerRadius = 30 postImg.clipsToBounds = true imgPicker = UIImagePickerController() imgPicker.delegate = self } 

这是我对didFinishPickingImage函数:

 func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) { imgPicker.dismissViewControllerAnimated(true, completion: nil) postImg.image = image } 

我在这里看了类似的post,但解决方案似乎没有用。
有什么建议么?

谢谢

UIImage包含一个名为imageOrientation的属性。由于Apple文档中描述了此属性:

图像方向会影响绘制时图像数据的显示方式。 默认情况下,图像以“向上”方向显示。 但是,如果图像具有关联的元数据(例如EXIF信息),则此属性包含该元数据指示的方向。

因此,此属性用于反映摄像机相对于地面的位置。因此,当您从手机摄像头拍摄图像时,它会将方向标志保存在UIImage imageOrientation属性中。
在显示是否忽略此属性时,您可能无法找到正确方向的图像。仅供参考,请参阅下图,相机如何拍摄图像然后进行编码和显示。请查看此链接以获取更多详细信息。 在此处输入图像描述

在ios中修复此问题
Objective-C的

制作类别

的UIImage + fixOrientation.h

 @interface UIImage (fixOrientation) - (UIImage *)fixOrientation; @end 

的UIImage + fixOrientation.m

 @implementation UIImage (fixOrientation) - (UIImage *)fixOrientation { // No-op if the orientation is already correct if (self.imageOrientation == UIImageOrientationUp) return self; // 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. CGAffineTransform transform = CGAffineTransformIdentity; switch (self.imageOrientation) { case UIImageOrientationDown: case UIImageOrientationDownMirrored: transform = CGAffineTransformTranslate(transform, self.size.width, self.size.height); transform = CGAffineTransformRotate(transform, M_PI); break; case UIImageOrientationLeft: case UIImageOrientationLeftMirrored: transform = CGAffineTransformTranslate(transform, self.size.width, 0); transform = CGAffineTransformRotate(transform, M_PI_2); break; case UIImageOrientationRight: case UIImageOrientationRightMirrored: transform = CGAffineTransformTranslate(transform, 0, self.size.height); transform = CGAffineTransformRotate(transform, -M_PI_2); break; case UIImageOrientationUp: case UIImageOrientationUpMirrored: break; } switch (self.imageOrientation) { case UIImageOrientationUpMirrored: case UIImageOrientationDownMirrored: transform = CGAffineTransformTranslate(transform, self.size.width, 0); transform = CGAffineTransformScale(transform, -1, 1); break; case UIImageOrientationLeftMirrored: case UIImageOrientationRightMirrored: transform = CGAffineTransformTranslate(transform, self.size.height, 0); transform = CGAffineTransformScale(transform, -1, 1); break; case UIImageOrientationUp: case UIImageOrientationDown: case UIImageOrientationLeft: case UIImageOrientationRight: break; } // Now we draw the underlying CGImage into a new context, applying the transform // calculated above. CGContextRef ctx = CGBitmapContextCreate(NULL, self.size.width, self.size.height, CGImageGetBitsPerComponent(self.CGImage), 0, CGImageGetColorSpace(self.CGImage), CGImageGetBitmapInfo(self.CGImage)); CGContextConcatCTM(ctx, transform); switch (self.imageOrientation) { case UIImageOrientationLeft: case UIImageOrientationLeftMirrored: case UIImageOrientationRight: case UIImageOrientationRightMirrored: // Grr... CGContextDrawImage(ctx, CGRectMake(0,0,self.size.height,self.size.width), self.CGImage); break; default: CGContextDrawImage(ctx, CGRectMake(0,0,self.size.width,self.size.height), self.CGImage); break; } // And now we just create a new UIImage from the drawing context CGImageRef cgimg = CGBitmapContextCreateImage(ctx); UIImage *img = [UIImage imageWithCGImage:cgimg]; CGContextRelease(ctx); CGImageRelease(cgimg); return img; } @end 

迅速

扩展为

 extension UIImage { func fixOrientation() -> UIImage { // No-op if the orientation is already correct if ( self.imageOrientation == UIImageOrientation.Up ) { return self; } // 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 if ( self.imageOrientation == UIImageOrientation.Down || self.imageOrientation == UIImageOrientation.DownMirrored ) { transform = CGAffineTransformTranslate(transform, self.size.width, self.size.height) transform = CGAffineTransformRotate(transform, CGFloat(M_PI)) } if ( self.imageOrientation == UIImageOrientation.Left || self.imageOrientation == UIImageOrientation.LeftMirrored ) { transform = CGAffineTransformTranslate(transform, self.size.width, 0) transform = CGAffineTransformRotate(transform, CGFloat(M_PI_2)) } if ( self.imageOrientation == UIImageOrientation.Right || self.imageOrientation == UIImageOrientation.RightMirrored ) { transform = CGAffineTransformTranslate(transform, 0, self.size.height); transform = CGAffineTransformRotate(transform, CGFloat(-M_PI_2)); } if ( self.imageOrientation == UIImageOrientation.UpMirrored || self.imageOrientation == UIImageOrientation.DownMirrored ) { transform = CGAffineTransformTranslate(transform, self.size.width, 0) transform = CGAffineTransformScale(transform, -1, 1) } if ( self.imageOrientation == UIImageOrientation.LeftMirrored || self.imageOrientation == UIImageOrientation.RightMirrored ) { transform = CGAffineTransformTranslate(transform, self.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(self.size.width), Int(self.size.height), CGImageGetBitsPerComponent(self.CGImage), 0, CGImageGetColorSpace(self.CGImage), CGImageGetBitmapInfo(self.CGImage).rawValue)!; CGContextConcatCTM(ctx, transform) if ( self.imageOrientation == UIImageOrientation.Left || self.imageOrientation == UIImageOrientation.LeftMirrored || self.imageOrientation == UIImageOrientation.Right || self.imageOrientation == UIImageOrientation.RightMirrored ) { CGContextDrawImage(ctx, CGRectMake(0,0,self.size.height,self.size.width), self.CGImage) } else { CGContextDrawImage(ctx, CGRectMake(0,0,self.size.width,self.size.height), self.CGImage) } // And now we just create a new UIImage from the drawing context and return it return UIImage(CGImage: CGBitmapContextCreateImage(ctx)!) } } 

Sunil Sharma的解决方案对我有用。 这是Swift 4的翻译版本

 import UIKit import CoreGraphics extension UIImage { func fixOrientation() -> UIImage { guard imageOrientation != .up else { return self } var transform: CGAffineTransform = .identity switch imageOrientation { case .down, .downMirrored: transform = transform.translatedBy(x: size.width ,y: size.height).rotated(by: .pi) case .left, .leftMirrored: transform = transform.translatedBy(x: size.width, y: 0).rotated(by: .pi) case .right, .rightMirrored: transform = transform.translatedBy(x: 0, y: size.height).rotated(by: -.pi/2) case .upMirrored: transform = transform.translatedBy(x: size.width, y: 0).scaledBy(x: -1, y: 1) default: break } guard let cgImage = cgImage, let colorSpace = cgImage.colorSpace, let context: CGContext = CGContext(data: nil, width: Int(size.width), height: Int(size.height), bitsPerComponent: cgImage.bitsPerComponent, bytesPerRow: 0, space: colorSpace, bitmapInfo: cgImage.bitmapInfo.rawValue) else { return self } context.concatenate(transform) var rect: CGRect switch imageOrientation { case .left, .leftMirrored, .right, .rightMirrored: rect = CGRect(x: 0, y: 0, width: size.height, height: size.width) default: rect = CGRect(x: 0, y: 0, width: size.width, height: size.height) } context.draw(cgImage, in: rect) guard let image = context.makeImage() else { return self } return UIImage(cgImage: image) } } 

你可以像这篇文章一样旋转图像: https : //stackoverflow.com/a/1317194/3928412

你需要让设备旋转,你可以得到它:

  UIDevice.currentDevice().orientation