UIImage
may have a different orientation depending on the rotation of the camera. You can dynamically enable the transformation that you want to apply to the image depending on this orientation, for example:
let renderer = UIGraphicsImageRenderer(size: image.size, format: format) let imageData = renderer.jpegData(withCompressionQuality: 1, actions: { context in var workSize = image.size; workSize.width = floor(workSize.width / image.scale) workSize.height = floor(workSize.height / image.scale) // No-op if the orientation is already correct // if image.imageOrientation == .up { draw image } // 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.identity switch image.imageOrientation { case .down, .downMirrored: transform = transform.translatedBy(x: workSize.width, y: workSize.height) transform = transform.rotated(by: CGFloat(Double.pi)) break case .left, .leftMirrored: transform = transform.translatedBy(x: workSize.width, y: 0.0) transform = transform.rotated(by: CGFloat(Double.pi / 2.0)) break case .right, .rightMirrored: transform = transform.translatedBy(x: 0.0, y: workSize.height) transform = transform.rotated(by: CGFloat(-Double.pi / 2.0)) break case .up, .upMirrored: break } switch image.imageOrientation { case .upMirrored, .downMirrored: transform = transform.translatedBy(x: workSize.width, y: 0.0) transform = transform.scaledBy(x: -1.0, y: 1.0) break case .leftMirrored, .rightMirrored: transform = transform.translatedBy(x: workSize.height, y: 0.0); transform = transform.scaledBy(x: -1.0, y: 1.0); break case .up, .down, .left, .right: break } // Now we draw the underlying CGImage into a new context, applying the transform // calculated above. let ctx = context.cgContext ctx.concatenate(transform) switch image.imageOrientation { case .left, .leftMirrored, .right, .rightMirrored: ctx.draw(image.cgImage!, in: CGRect(x: 0.0, y:0.0, width: workSize.height, height: workSize.width)) break; default: ctx.draw(image.cgImage!, in: CGRect(origin: .zero, size: workSize)) break; } })
Answer based on UIImage + fixOrientation
source share