Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save olgusirman/3ae5cb7b0e746d9ea792e389e904336d to your computer and use it in GitHub Desktop.
Save olgusirman/3ae5cb7b0e746d9ea792e389e904336d to your computer and use it in GitHub Desktop.
Sample helper code which decides UIImage orientation
extension UIImage {
func rotateCameraImageToProperOrientation(maxResolution: CGFloat) -> UIImage {
let imgRef: CGImage = cgImage!
let width: CGFloat = CGFloat(imgRef.width)
let height: CGFloat = CGFloat(imgRef.height)
var bounds: CGRect = CGRect(x: 0, y: 0, width: width, height: height)
var scaleRatio: CGFloat = 1
if width > maxResolution || height > maxResolution {
scaleRatio = min(maxResolution / bounds.size.width, maxResolution / bounds.size.height)
bounds.size.height = bounds.size.height * scaleRatio
bounds.size.width = bounds.size.width * scaleRatio
}
var transform: CGAffineTransform = .identity
let orient: UIImageOrientation = imageOrientation
let imageSize: CGSize = CGSize(width: imgRef.width, height: imgRef.height)
switch imageOrientation {
case .up :
transform = .identity
case .upMirrored :
transform = CGAffineTransform(translationX: imageSize.width, y: 0)
transform = transform.scaledBy(x: -1, y: 1)
case .down :
transform = CGAffineTransform(translationX: imageSize.width, y: imageSize.height)
transform = transform.rotated(by: CGFloat.pi)
case .downMirrored :
transform = CGAffineTransform(translationX: 0, y: imageSize.height)
transform = transform.scaledBy(x: 1, y: -1)
case .left :
let storedHeight = bounds.size.height
bounds.size.height = bounds.size.width
bounds.size.width = storedHeight
transform = CGAffineTransform(translationX: 0, y: imageSize.width)
transform = transform.rotated(by: 3.0 * CGFloat.pi / 2.0)
case .leftMirrored :
let storedHeight = bounds.size.height
bounds.size.height = bounds.size.width
bounds.size.width = storedHeight
transform = CGAffineTransform(translationX: imageSize.height, y: imageSize.width)
transform = transform.scaledBy(x: -1, y: 1)
transform = transform.rotated(by: 3.0 * CGFloat.pi / 2.0)
case .right :
let storedHeight = bounds.size.height
bounds.size.height = bounds.size.width
bounds.size.width = storedHeight
transform = CGAffineTransform(translationX: imageSize.height, y: 0)
transform = transform.rotated(by: CGFloat.pi / 2.0)
case .rightMirrored :
let storedHeight: CGFloat = bounds.size.height
bounds.size.height = bounds.size.width
bounds.size.width = storedHeight
transform = CGAffineTransform(scaleX: -1, y: 1)
transform = transform.rotated(by: CGFloat.pi / 2.0)
}
UIGraphicsBeginImageContext(bounds.size)
let context = UIGraphicsGetCurrentContext()
if orient == .right || orient == .left {
context!.scaleBy(x: -scaleRatio, y: scaleRatio)
context!.translateBy(x: -height, y: 0)
} else {
context!.scaleBy(x: scaleRatio, y: -scaleRatio)
context!.translateBy(x: 0, y: -height)
}
context!.concatenate(transform)
context!.draw(imgRef, in: CGRect(x: 0, y: 0, width: width, height: height))
let imageCopy = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return imageCopy!
}
}
//https://geek-is-stupid.github.io/2017-04-11-uiimage-display-wrong-orientation-what-is-solution/
// Usage
let maxResolution: CGFloat = max(yourImage.size.width, yourImage.size.height)
let updatedImage: UIImage = yourImage.rotateCameraImageToProperOrientation(maxResolution: maxResolution)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment