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

Commit 4fbc52b

Browse files
Merge branch 'release/1.4.0'
2 parents 52d29ad + 077ac04 commit 4fbc52b

File tree

7 files changed

+421
-292
lines changed

7 files changed

+421
-292
lines changed

ALCameraViewController.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |spec|
22
spec.name = "ALCameraViewController"
3-
spec.version = "1.3.1"
3+
spec.version = "1.4.0"
44
spec.summary = "A camera view controller with custom image picker and image cropping. Written in Swift."
55
spec.source = { :git => "https://github.com/AlexLittlejohn/ALCameraViewController.git", :tag => spec.version.to_s }
66
spec.requires_arc = true

ALCameraViewController/Utilities/CameraShot.swift

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import AVFoundation
1111

1212
public typealias CameraShotCompletion = (UIImage?) -> Void
1313

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

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

2525
guard let buffer = buffer,
2626
let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(buffer),
27-
let image = UIImage(data: imageData) else {
27+
var image = UIImage(data: imageData) else {
2828
completion(nil)
2929
return
3030
}
31+
32+
// flip the image to match the orientation of the preview
33+
if cameraPosition == .front, let cgImage = image.cgImage {
34+
switch image.imageOrientation {
35+
case .leftMirrored:
36+
image = UIImage(cgImage: cgImage, scale: image.scale, orientation: .right)
37+
case .left:
38+
image = UIImage(cgImage: cgImage, scale: image.scale, orientation: .rightMirrored)
39+
case .rightMirrored:
40+
image = UIImage(cgImage: cgImage, scale: image.scale, orientation: .left)
41+
case .right:
42+
image = UIImage(cgImage: cgImage, scale: image.scale, orientation: .leftMirrored)
43+
case .up:
44+
image = UIImage(cgImage: cgImage, scale: image.scale, orientation: .upMirrored)
45+
case .upMirrored:
46+
image = UIImage(cgImage: cgImage, scale: image.scale, orientation: .up)
47+
case .down:
48+
image = UIImage(cgImage: cgImage, scale: image.scale, orientation: .downMirrored)
49+
case .downMirrored:
50+
image = UIImage(cgImage: cgImage, scale: image.scale, orientation: .down)
51+
}
52+
}
3153

3254
completion(image)
3355
})

ALCameraViewController/Utilities/UIViewExtensions.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import UIKit
33
extension UIView {
44
func autoRemoveConstraint(_ constraint : NSLayoutConstraint?) {
55
if constraint != nil {
6-
self.removeConstraint(constraint!)
6+
removeConstraint(constraint!)
77
}
88
}
99
}

ALCameraViewController/Utilities/Utilities.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
import UIKit
1010
import AVFoundation
1111

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

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

27-
internal func currentRotation(_ oldOrientation: UIInterfaceOrientation, newOrientation: UIInterfaceOrientation) -> Double {
27+
internal func currentRotation(_ oldOrientation: UIInterfaceOrientation, newOrientation: UIInterfaceOrientation) -> CGFloat {
2828
switch oldOrientation {
2929
case .portrait:
3030
switch newOrientation {

ALCameraViewController/ViewController/CameraViewController.swift

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -155,16 +155,21 @@ open class CameraViewController: UIViewController {
155155
view.translatesAutoresizingMaskIntoConstraints = false
156156
return view
157157
}()
158+
159+
private let allowsLibraryAccess: Bool
158160

159-
public init(croppingEnabled: Bool, allowsLibraryAccess: Bool = true, completion: @escaping CameraViewCompletion) {
161+
public init(croppingEnabled: Bool, allowsLibraryAccess: Bool = true, allowsSwapCameraOrientation: Bool = true, completion: @escaping CameraViewCompletion) {
162+
self.allowsLibraryAccess = allowsLibraryAccess
160163
super.init(nibName: nil, bundle: nil)
161164
onCompletion = completion
162165
allowCropping = croppingEnabled
163166
cameraOverlay.isHidden = !allowCropping
164167
libraryButton.isEnabled = allowsLibraryAccess
165168
libraryButton.isHidden = !allowsLibraryAccess
169+
swapButton.isEnabled = allowsSwapCameraOrientation
170+
swapButton.isHidden = !allowsSwapCameraOrientation
166171
}
167-
172+
168173
required public init?(coder aDecoder: NSCoder) {
169174
fatalError("init(coder:) has not been implemented")
170175
}
@@ -386,8 +391,8 @@ open class CameraViewController: UIViewController {
386391
internal func rotate(actualInterfaceOrientation: UIInterfaceOrientation) {
387392

388393
if lastInterfaceOrientation != nil {
389-
let lastTransform = CGAffineTransform(rotationAngle: CGFloat(radians(currentRotation(
390-
lastInterfaceOrientation!, newOrientation: actualInterfaceOrientation))))
394+
let lastTransform = CGAffineTransform(rotationAngle: radians(currentRotation(
395+
lastInterfaceOrientation!, newOrientation: actualInterfaceOrientation)))
391396
setTransform(transform: lastTransform)
392397
}
393398

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

510+
if allowsLibraryAccess {
505511
_ = SingleImageSaver()
506512
.setImage(image)
507513
.onSuccess { [weak self] asset in
@@ -515,8 +521,12 @@ open class CameraViewController: UIViewController {
515521
self?.hideSpinner(spinner)
516522
}
517523
.save()
524+
} else {
525+
layoutCameraResult(uiImage: image)
526+
hideSpinner(spinner)
527+
}
518528
}
519-
529+
520530
internal func close() {
521531
onCompletion?(nil, nil)
522532
onCompletion = nil
@@ -558,13 +568,37 @@ open class CameraViewController: UIViewController {
558568
cameraView.swapCameraInput()
559569
flashButton.isHidden = cameraView.currentPosition == AVCaptureDevicePosition.front
560570
}
561-
571+
572+
internal func layoutCameraResult(uiImage: UIImage) {
573+
cameraView.stopSession()
574+
startConfirmController(uiImage: uiImage)
575+
toggleButtons(enabled: true)
576+
}
577+
562578
internal func layoutCameraResult(asset: PHAsset) {
563579
cameraView.stopSession()
564580
startConfirmController(asset: asset)
565581
toggleButtons(enabled: true)
566582
}
567-
583+
584+
private func startConfirmController(uiImage: UIImage) {
585+
let confirmViewController = ConfirmViewController(image: uiImage, allowsCropping: allowCropping)
586+
confirmViewController.onComplete = { [weak self] image, asset in
587+
defer {
588+
self?.dismiss(animated: true, completion: nil)
589+
}
590+
591+
guard let image = image else {
592+
return
593+
}
594+
595+
self?.onCompletion?(image, asset)
596+
self?.onCompletion = nil
597+
}
598+
confirmViewController.modalTransitionStyle = UIModalTransitionStyle.crossDissolve
599+
present(confirmViewController, animated: true, completion: nil)
600+
}
601+
568602
private func startConfirmController(asset: PHAsset) {
569603
let confirmViewController = ConfirmViewController(asset: asset, allowsCropping: allowCropping)
570604
confirmViewController.onComplete = { [weak self] image, asset in

0 commit comments

Comments
 (0)