Skip to content

Instantly share code, notes, and snippets.

@algal
Last active August 30, 2016 15:45
Show Gist options
  • Save algal/c687e9b90c957b2ab7ab76dab1204ae0 to your computer and use it in GitHub Desktop.
Save algal/c687e9b90c957b2ab7ab76dab1204ae0 to your computer and use it in GitHub Desktop.
What orientation value re-orients raw images?
import UIKit
import AVFoundation
/** Returns a UIImageOrientation that will adjust an image
- parameter deviceOrientation: physical orientation of the device when the image was captured. Must not be FaceUp or FaceDown
- parameter position: position of the camera used for capture. Must be .Front or .Back
- returns an UIImageOrientation, which can be used when initializing a UIImageView.
*/
func imageOrientation(forDevicePosition position:AVCaptureDevicePosition,
deviceOrientation:UIDeviceOrientation) -> UIImageOrientation
{
switch (deviceOrientation,position) {
case (.LandscapeRight,.Back): return .Up
case (.LandscapeRight,.Front): return .Down
case (.LandscapeLeft,.Back): return .Down
case (.LandscapeLeft,.Front): return .Up
case (.Portrait,.Back): return .Right
case (.Portrait,.Front): return .Right
case (.PortraitUpsideDown,.Back): return .Left
case (.PortraitUpsideDown,.Front): return .Left
case (.FaceUp,_): fallthrough
case (.FaceDown,_): fallthrough
case (_,.Unspecified): fallthrough
default:
NSLog("Called in unrecognized orientation")
return .Left
}
}
enum EXIFOrientation : Int32 {
case topLeft = 1
case topRight
case bottomRight
case bottomLeft
case leftTop
case rightTop
case rightBottom
case leftBottom
var isReflect:Bool {
switch self {
case topLeft,bottomRight,rightTop,leftBottom: return false
default: return true
}
}
}
/**
Returns EXIFOrientation that will adjust an image.
- parameter deviceOrientation: physical orientation of the device when the image was captured. Must not be FaceUp or FaceDown
- parameter position: position of the camera used for capture. Must be .Front or .Back
- returns an EXIFOrientation, whose rawValue can be used as a CGImagePropertyOrientation,
*/
func compensatingEXIFOrientation(forDevicePosition position:AVCaptureDevicePosition,
deviceOrientation:UIDeviceOrientation) -> EXIFOrientation
{
switch (deviceOrientation,position) {
case (.LandscapeRight,.Back): return .topLeft
case (.LandscapeRight,.Front): return .bottomRight
case (.LandscapeLeft,.Back): return .bottomRight
case (.LandscapeLeft,.Front): return .topLeft
case (.Portrait,.Back): return .rightTop
case (.Portrait,.Front): return .rightTop
case (.PortraitUpsideDown,.Back): return .leftBottom
case (.PortraitUpsideDown,.Front): return .leftBottom
case (.FaceUp,_): fallthrough
case (.FaceDown,_): fallthrough
case (_,.Unspecified): fallthrough
default:
NSLog("Called in unrecognized orientation")
return .rightTop
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment