Commit d4aada39 by Daniel Dahan

moved adjustOrientation UIImage helper to UIImage instance methods

parent ffd980fe
...@@ -575,7 +575,7 @@ public class CaptureSession: NSObject, AVCaptureFileOutputRecordingDelegate { ...@@ -575,7 +575,7 @@ public class CaptureSession: NSObject, AVCaptureFileOutputRecordingDelegate {
if nil == captureError { if nil == captureError {
let data: NSData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sampleBuffer) let data: NSData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sampleBuffer)
if let image1: UIImage = UIImage(data: data as Data) { if let image1: UIImage = UIImage(data: data as Data) {
if let image2: UIImage = s.adjustOrientationForImage(image: image1) { if let image2: UIImage = image1.adjustOrientation() {
s.delegate?.captureSessionStillImageAsynchronously?(captureSession: s, image: image2) s.delegate?.captureSessionStillImageAsynchronously?(captureSession: s, image: image2)
} else { } else {
var userInfo: Dictionary<String, AnyObject> = Dictionary<String, AnyObject>() var userInfo: Dictionary<String, AnyObject> = Dictionary<String, AnyObject>()
...@@ -732,64 +732,4 @@ public class CaptureSession: NSObject, AVCaptureFileOutputRecordingDelegate { ...@@ -732,64 +732,4 @@ public class CaptureSession: NSObject, AVCaptureFileOutputRecordingDelegate {
} }
return nil return nil
} }
/**
Adjusts the orientation of the image from the capture orientation.
This is an issue when taking images, the capture orientation is not set correctly
when using Portrait.
- Parameter image: A UIImage to adjust.
- Returns: An optional UIImage if successful.
*/
private func adjustOrientationForImage(image: UIImage) -> UIImage? {
guard .up != image.imageOrientation else {
return image
}
var transform: CGAffineTransform = .identity
// Rotate if Left, Right, or Down.
switch image.imageOrientation {
case .down, .downMirrored:
transform = transform.translateBy(x: image.size.width, y: image.size.height)
transform = transform.rotate(CGFloat(M_PI))
case .left, .leftMirrored:
transform = transform.translateBy(x: image.size.width, y: 0)
transform = transform.rotate(CGFloat(M_PI_2))
case .right, .rightMirrored:
transform = transform.translateBy(x: 0, y: image.size.height)
transform = transform.rotate(-CGFloat(M_PI_2))
default:break
}
// Flip if mirrored.
switch image.imageOrientation {
case .upMirrored, .downMirrored:
transform = transform.translateBy(x: image.size.width, y: 0)
transform = transform.scaleBy(x: -1, y: 1)
case .leftMirrored, .rightMirrored:
transform = transform.translateBy(x: image.size.height, y: 0)
transform = transform.scaleBy(x: -1, y: 1)
default:break
}
// Draw the underlying cgImage with the calculated transform.
guard let context = CGContext(data: nil, width: Int(image.size.width), height: Int(image.size.height), bitsPerComponent: image.cgImage!.bitsPerComponent, bytesPerRow: 0, space: image.cgImage!.colorSpace!, bitmapInfo: image.cgImage!.bitmapInfo.rawValue) else {
return nil
}
context.concatCTM(transform)
switch image.imageOrientation {
case .left, .leftMirrored, .right, .rightMirrored:
context.draw(in: CGRect(x: 0, y: 0, width: image.size.height, height: image.size.width), image: image.cgImage!)
default:
context.draw(in: CGRect(origin: .zero, size: image.size), image: image.cgImage!)
}
guard let cgImage = context.makeImage() else {
return nil
}
return UIImage(cgImage: cgImage)
}
} }
...@@ -177,4 +177,63 @@ public extension UIImage { ...@@ -177,4 +177,63 @@ public extension UIImage {
} }
}.resume() }.resume()
} }
/**
Adjusts the orientation of the image from the capture orientation.
This is an issue when taking images, the capture orientation is not set correctly
when using Portrait.
- Returns: An optional UIImage if successful.
*/
public func adjustOrientation() -> UIImage? {
guard .up != imageOrientation else {
return self
}
var transform: CGAffineTransform = .identity
// Rotate if Left, Right, or Down.
switch imageOrientation {
case .down, .downMirrored:
transform = transform.translateBy(x: size.width, y: size.height)
transform = transform.rotate(CGFloat(M_PI))
case .left, .leftMirrored:
transform = transform.translateBy(x: size.width, y: 0)
transform = transform.rotate(CGFloat(M_PI_2))
case .right, .rightMirrored:
transform = transform.translateBy(x: 0, y: size.height)
transform = transform.rotate(-CGFloat(M_PI_2))
default:break
}
// Flip if mirrored.
switch imageOrientation {
case .upMirrored, .downMirrored:
transform = transform.translateBy(x: size.width, y: 0)
transform = transform.scaleBy(x: -1, y: 1)
case .leftMirrored, .rightMirrored:
transform = transform.translateBy(x: size.height, y: 0)
transform = transform.scaleBy(x: -1, y: 1)
default:break
}
// Draw the underlying cgImage with the calculated transform.
guard let context = CGContext(data: nil, width: Int(size.width), height: Int(size.height), bitsPerComponent: cgImage!.bitsPerComponent, bytesPerRow: 0, space: cgImage!.colorSpace!, bitmapInfo: cgImage!.bitmapInfo.rawValue) else {
return nil
}
context.concatCTM(transform)
switch imageOrientation {
case .left, .leftMirrored, .right, .rightMirrored:
context.draw(in: CGRect(x: 0, y: 0, width: size.height, height: size.width), image: cgImage!)
default:
context.draw(in: CGRect(origin: .zero, size: size), image: cgImage!)
}
guard let cgImage = context.makeImage() else {
return nil
}
return UIImage(cgImage: cgImage)
}
} }
...@@ -131,24 +131,6 @@ public class View: UIView { ...@@ -131,24 +131,6 @@ public class View: UIView {
} }
/** /**
A property that manages the overall shape for the object. If either the
width or height property is set, the other will be automatically adjusted
to maintain the shape of the object.
*/
public var shape: ShapePreset = .none {
didSet {
if .none != shape {
if width < height {
frame.size.width = height
} else {
frame.size.height = width
}
layoutShadowPath()
}
}
}
/**
An initializer that initializes the object with a NSCoder object. An initializer that initializes the object with a NSCoder object.
- Parameter aDecoder: A NSCoder instance. - Parameter aDecoder: A NSCoder instance.
*/ */
...@@ -217,7 +199,7 @@ public class View: UIView { ...@@ -217,7 +199,7 @@ public class View: UIView {
/// Manages the layout for the shape of the view instance. /// Manages the layout for the shape of the view instance.
internal func layoutShape() { internal func layoutShape() {
if .circle == shape { if .circle == shapePreset {
let w: CGFloat = (width / 2) let w: CGFloat = (width / 2)
if w != cornerRadius { if w != cornerRadius {
cornerRadius = w cornerRadius = w
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment