Skip to content

Commit

Permalink
Rotate image/video based on device orientation
Browse files Browse the repository at this point in the history
Resolves #8
  • Loading branch information
Adrian Mateoaea committed Feb 27, 2019
1 parent 22c9259 commit 214676a
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 5 deletions.
10 changes: 9 additions & 1 deletion CameraKit/CameraKit/CKFPhotoSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,14 @@ extension CKFSession.FlashMode {
let settings = AVCapturePhotoSettings()
settings.flashMode = self.flashMode.captureFlashMode

if let connection = self.photoOutput.connection(with: .video) {
if self.resolution.width > 0, self.resolution.height > 0 {
connection.videoOrientation = .portrait
} else {
connection.videoOrientation = UIDevice.current.orientation.videoOrientation
}
}

self.photoOutput.capturePhoto(with: settings, delegate: self)
}

Expand Down Expand Up @@ -224,7 +232,7 @@ extension CKFSession.FlashMode {

if
self.resolution.width > 0, self.resolution.height > 0,
let transformedImage = CKFUtils.cropAndScale(image, width: Int(self.resolution.width), height: Int(self.resolution.height))
let transformedImage = CKUtils.cropAndScale(image, width: Int(self.resolution.width), height: Int(self.resolution.height), orientation: UIDevice.current.orientation, mirrored: self.cameraPosition == .front)
{
self.captureCallback(transformedImage, resolvedSettings)
return
Expand Down
16 changes: 16 additions & 0 deletions CameraKit/CameraKit/CKFSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,22 @@

import AVFoundation

public extension UIDeviceOrientation {

var videoOrientation: AVCaptureVideoOrientation {
switch UIDevice.current.orientation {
case .portraitUpsideDown:
return .portraitUpsideDown
case .landscapeLeft:
return .landscapeRight
case .landscapeRight:
return .landscapeLeft
default:
return .portrait
}
}
}

private extension CKFSession.DeviceType {

var captureDeviceType: AVCaptureDevice.DeviceType {
Expand Down
37 changes: 33 additions & 4 deletions CameraKit/CameraKit/CKFUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,37 @@

import UIKit

@objc public class CKFUtils: NSObject {
private extension UIDeviceOrientation {
var imageOrientation: UIImageOrientation {
switch self {
case .portraitUpsideDown:
return .left
case .landscapeLeft:
return .up
case .landscapeRight:
return .down
default:
return .right
}
}

@objc public static func cropAndScale(_ image: UIImage, width: Int, height: Int) -> UIImage? {
var imageOrientationMirrored: UIImageOrientation {
switch self {
case .portraitUpsideDown:
return .left
case .landscapeLeft:
return .down
case .landscapeRight:
return .up
default:
return .right
}
}
}

@objc public class CKUtils: NSObject {

@objc public static func cropAndScale(_ image: UIImage, width: Int, height: Int, orientation: UIDeviceOrientation, mirrored: Bool) -> UIImage? {
let fromRect = CGRect(x: 0, y: 0, width: image.size.height, height: image.size.width)
var toRect = CGRect(x: 0, y: 0, width: height, height: width)

Expand Down Expand Up @@ -40,7 +68,8 @@ import UIKit
guard let finalCgImage = context.makeImage() else {
return nil
}

return UIImage(cgImage: finalCgImage, scale: 1.0, orientation: .right)

let orientation = mirrored ? orientation.imageOrientationMirrored : orientation.imageOrientation
return UIImage(cgImage: finalCgImage, scale: 1.0, orientation: orientation)
}
}
4 changes: 4 additions & 0 deletions CameraKit/CameraKit/CKFVideoSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ extension CKFSession.FlashMode {
return fileUrl
}()

if let connection = self.movieOutput.connection(with: .video) {
connection.videoOrientation = UIDevice.current.orientation.videoOrientation
}

self.movieOutput.startRecording(to: fileUrl, recordingDelegate: self)
}

Expand Down

0 comments on commit 214676a

Please sign in to comment.