Skip to content
This repository has been archived by the owner on Jul 1, 2022. It is now read-only.

Commit

Permalink
Merge branch 'release/1.4.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexLittlejohn committed Jul 29, 2017
2 parents 52d29ad + 077ac04 commit 4fbc52b
Show file tree
Hide file tree
Showing 7 changed files with 421 additions and 292 deletions.
2 changes: 1 addition & 1 deletion ALCameraViewController.podspec
@@ -1,6 +1,6 @@
Pod::Spec.new do |spec|
spec.name = "ALCameraViewController"
spec.version = "1.3.1"
spec.version = "1.4.0"
spec.summary = "A camera view controller with custom image picker and image cropping. Written in Swift."
spec.source = { :git => "https://github.com/AlexLittlejohn/ALCameraViewController.git", :tag => spec.version.to_s }
spec.requires_arc = true
Expand Down
26 changes: 24 additions & 2 deletions ALCameraViewController/Utilities/CameraShot.swift
Expand Up @@ -11,7 +11,7 @@ import AVFoundation

public typealias CameraShotCompletion = (UIImage?) -> Void

public func takePhoto(_ stillImageOutput: AVCaptureStillImageOutput, videoOrientation: AVCaptureVideoOrientation, cropSize: CGSize, completion: @escaping CameraShotCompletion) {
public func takePhoto(_ stillImageOutput: AVCaptureStillImageOutput, videoOrientation: AVCaptureVideoOrientation, cameraPosition: AVCaptureDevicePosition, cropSize: CGSize, completion: @escaping CameraShotCompletion) {

guard let videoConnection: AVCaptureConnection = stillImageOutput.connection(withMediaType: AVMediaTypeVideo) else {
completion(nil)
Expand All @@ -24,10 +24,32 @@ public func takePhoto(_ stillImageOutput: AVCaptureStillImageOutput, videoOrient

guard let buffer = buffer,
let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(buffer),
let image = UIImage(data: imageData) else {
var image = UIImage(data: imageData) else {
completion(nil)
return
}

// flip the image to match the orientation of the preview
if cameraPosition == .front, let cgImage = image.cgImage {
switch image.imageOrientation {
case .leftMirrored:
image = UIImage(cgImage: cgImage, scale: image.scale, orientation: .right)
case .left:
image = UIImage(cgImage: cgImage, scale: image.scale, orientation: .rightMirrored)
case .rightMirrored:
image = UIImage(cgImage: cgImage, scale: image.scale, orientation: .left)
case .right:
image = UIImage(cgImage: cgImage, scale: image.scale, orientation: .leftMirrored)
case .up:
image = UIImage(cgImage: cgImage, scale: image.scale, orientation: .upMirrored)
case .upMirrored:
image = UIImage(cgImage: cgImage, scale: image.scale, orientation: .up)
case .down:
image = UIImage(cgImage: cgImage, scale: image.scale, orientation: .downMirrored)
case .downMirrored:
image = UIImage(cgImage: cgImage, scale: image.scale, orientation: .down)
}
}

completion(image)
})
Expand Down
2 changes: 1 addition & 1 deletion ALCameraViewController/Utilities/UIViewExtensions.swift
Expand Up @@ -3,7 +3,7 @@ import UIKit
extension UIView {
func autoRemoveConstraint(_ constraint : NSLayoutConstraint?) {
if constraint != nil {
self.removeConstraint(constraint!)
removeConstraint(constraint!)
}
}
}
6 changes: 3 additions & 3 deletions ALCameraViewController/Utilities/Utilities.swift
Expand Up @@ -9,8 +9,8 @@
import UIKit
import AVFoundation

internal func radians(_ degrees: Double) -> Double {
return degrees / 180 * Double.pi
internal func radians(_ degrees: CGFloat) -> CGFloat {
return degrees / 180 * .pi
}

internal func localizedString(_ key: String) -> String {
Expand All @@ -24,7 +24,7 @@ internal func localizedString(_ key: String) -> String {
return NSLocalizedString(key, tableName: CameraGlobals.shared.stringsTable, bundle: bundle, comment: key)
}

internal func currentRotation(_ oldOrientation: UIInterfaceOrientation, newOrientation: UIInterfaceOrientation) -> Double {
internal func currentRotation(_ oldOrientation: UIInterfaceOrientation, newOrientation: UIInterfaceOrientation) -> CGFloat {
switch oldOrientation {
case .portrait:
switch newOrientation {
Expand Down
48 changes: 41 additions & 7 deletions ALCameraViewController/ViewController/CameraViewController.swift
Expand Up @@ -155,16 +155,21 @@ open class CameraViewController: UIViewController {
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()

private let allowsLibraryAccess: Bool

public init(croppingEnabled: Bool, allowsLibraryAccess: Bool = true, completion: @escaping CameraViewCompletion) {
public init(croppingEnabled: Bool, allowsLibraryAccess: Bool = true, allowsSwapCameraOrientation: Bool = true, completion: @escaping CameraViewCompletion) {
self.allowsLibraryAccess = allowsLibraryAccess
super.init(nibName: nil, bundle: nil)
onCompletion = completion
allowCropping = croppingEnabled
cameraOverlay.isHidden = !allowCropping
libraryButton.isEnabled = allowsLibraryAccess
libraryButton.isHidden = !allowsLibraryAccess
swapButton.isEnabled = allowsSwapCameraOrientation
swapButton.isHidden = !allowsSwapCameraOrientation
}
required public init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
Expand Down Expand Up @@ -386,8 +391,8 @@ open class CameraViewController: UIViewController {
internal func rotate(actualInterfaceOrientation: UIInterfaceOrientation) {

if lastInterfaceOrientation != nil {
let lastTransform = CGAffineTransform(rotationAngle: CGFloat(radians(currentRotation(
lastInterfaceOrientation!, newOrientation: actualInterfaceOrientation))))
let lastTransform = CGAffineTransform(rotationAngle: radians(currentRotation(
lastInterfaceOrientation!, newOrientation: actualInterfaceOrientation)))
setTransform(transform: lastTransform)
}

Expand Down Expand Up @@ -502,6 +507,7 @@ open class CameraViewController: UIViewController {
let spinner = showSpinner()
cameraView.preview.isHidden = true

if allowsLibraryAccess {
_ = SingleImageSaver()
.setImage(image)
.onSuccess { [weak self] asset in
Expand All @@ -515,8 +521,12 @@ open class CameraViewController: UIViewController {
self?.hideSpinner(spinner)
}
.save()
} else {
layoutCameraResult(uiImage: image)
hideSpinner(spinner)
}
}
internal func close() {
onCompletion?(nil, nil)
onCompletion = nil
Expand Down Expand Up @@ -558,13 +568,37 @@ open class CameraViewController: UIViewController {
cameraView.swapCameraInput()
flashButton.isHidden = cameraView.currentPosition == AVCaptureDevicePosition.front
}


internal func layoutCameraResult(uiImage: UIImage) {
cameraView.stopSession()
startConfirmController(uiImage: uiImage)
toggleButtons(enabled: true)
}

internal func layoutCameraResult(asset: PHAsset) {
cameraView.stopSession()
startConfirmController(asset: asset)
toggleButtons(enabled: true)
}


private func startConfirmController(uiImage: UIImage) {
let confirmViewController = ConfirmViewController(image: uiImage, allowsCropping: allowCropping)
confirmViewController.onComplete = { [weak self] image, asset in
defer {
self?.dismiss(animated: true, completion: nil)
}

guard let image = image else {
return
}

self?.onCompletion?(image, asset)
self?.onCompletion = nil
}
confirmViewController.modalTransitionStyle = UIModalTransitionStyle.crossDissolve
present(confirmViewController, animated: true, completion: nil)
}

private func startConfirmController(asset: PHAsset) {
let confirmViewController = ConfirmViewController(asset: asset, allowsCropping: allowCropping)
confirmViewController.onComplete = { [weak self] image, asset in
Expand Down

0 comments on commit 4fbc52b

Please sign in to comment.