Skip to content

Commit

Permalink
Updated to make video optional
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfej94 committed Feb 25, 2021
1 parent 1d25c3d commit bed4308
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 19 deletions.
2 changes: 1 addition & 1 deletion PhotographyKit.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Pod::Spec.new do |spec|

spec.name = "PhotographyKit"
spec.version = "0.23"
spec.version = "0.24"
spec.license = "MIT"
spec.summary = "A swift library for quickly integrating a built in camera session into your app."
spec.homepage = "https://github.com/appoly/PhotographyKit"
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@ Once you have your delegate setup, you can initialize your PhotographyKit object

- A view that will be used to display your camera preview
- The delegate which we declare above.
- Be aware that if your PhotographyKit object allows video then you will need microphone permissions

```
do {
camera = try PhotographyKit(view: captureView, delegate: self)
camera = try PhotographyKit(view: captureView, delegate: self, allowsVideo: true)
} catch let error {
showPhotographyKitError(error as? PhotographyKitError)
}
Expand Down
34 changes: 17 additions & 17 deletions Sources/PhotographyKit/PhotographyKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,15 @@ public class PhotographyKit: NSObject {

//User defined photo settings
private let zoomSensitivity: CGFloat = 0.03
private var delegate: PhotographyKitDelegate!
private let delegate: PhotographyKitDelegate
private let allowsVideo: Bool

//Capture session
private var captureSession: AVCaptureSession?
private let movieFileOutput = AVCaptureMovieFileOutput()
private let imageOutput = AVCapturePhotoOutput()
private var captureSession: AVCaptureSession?
private var timer: Timer?
private var videoPreviewLayer: AVCaptureVideoPreviewLayer?
private let imageOutput = AVCapturePhotoOutput()
private var captureDevice: AVCaptureDevice?
private var currentZoomFactor: CGFloat = 1
private var containingView: UIView?
Expand Down Expand Up @@ -102,6 +103,7 @@ public class PhotographyKit: NSObject {

/// Tries to start recording a video
public func startVideoRecording(maxLength: TimeInterval, url: URL? = nil) throws {
guard allowsVideo else { return }
timer?.invalidate()
if let safeURL = url == nil ? URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("\(UUID().uuidString).mp4") : url {
movieFileOutput.startRecording(to: safeURL, recordingDelegate: self)
Expand All @@ -115,6 +117,7 @@ public class PhotographyKit: NSObject {


public func endVideoRecording() {
guard allowsVideo else { return }
movieFileOutput.stopRecording()
}

Expand Down Expand Up @@ -223,10 +226,10 @@ public class PhotographyKit: NSObject {

// MARK: - Initializers

public init?(view: UIView, delegate: PhotographyKitDelegate) throws {
super.init()

public init?(view: UIView, delegate: PhotographyKitDelegate, allowsVideo: Bool) throws {
self.allowsVideo = allowsVideo
self.delegate = delegate
super.init()

view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(focus(_:))))
view.addGestureRecognizer(UIPinchGestureRecognizer(target: self, action: #selector(zoom(_:))))
Expand All @@ -248,14 +251,14 @@ public class PhotographyKit: NSObject {

private func setupCamera(view: UIView) throws {
let videoDeviceDiscoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInWideAngleCamera], mediaType: AVMediaType.video, position: .back)
let audioDeviceDiscoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInMicrophone], mediaType: AVMediaType.audio, position: .unspecified)
let audioDeviceDiscoverySession = allowsVideo ? AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInMicrophone], mediaType: AVMediaType.audio, position: .unspecified) : nil

guard let videoCaptureDevice = videoDeviceDiscoverySession.devices.first else {
throw PhotographyKitError.failedToConnectToDeviceCamera
}
guard let audioCaptureDevice = audioDeviceDiscoverySession.devices.first else {
throw PhotographyKitError.failedToConnectToDeviceMicrophone
}

let audioCaptureDevice = audioDeviceDiscoverySession?.devices.first

try videoCaptureDevice.lockForConfiguration()
defer { videoCaptureDevice.unlockForConfiguration() }
videoCaptureDevice.torchMode = .auto
Expand All @@ -264,15 +267,16 @@ public class PhotographyKit: NSObject {
}


private func setupPreview(view: UIView, videoCaptureDevice: AVCaptureDevice, audioCaptureDevice: AVCaptureDevice) throws {
private func setupPreview(view: UIView, videoCaptureDevice: AVCaptureDevice, audioCaptureDevice: AVCaptureDevice?) throws {
do {
let captureSession = AVCaptureSession()

guard let videoInput = try? AVCaptureDeviceInput(device: videoCaptureDevice) else {
throw PhotographyKitError.failedToConnectToDeviceCamera
}
guard let audioInput = try? AVCaptureDeviceInput(device: audioCaptureDevice) else {
throw PhotographyKitError.failedToConnectToDeviceMicrophone

if let audioCaptureDevice = audioCaptureDevice, let audioInput = try? AVCaptureDeviceInput(device: audioCaptureDevice), captureSession.canAddInput(audioInput) {
captureSession.addInput(audioInput)
}

if(captureSession.canAddInput(videoInput)) {
Expand All @@ -283,10 +287,6 @@ public class PhotographyKit: NSObject {
captureSession.addOutput(imageOutput)
}

if(captureSession.canAddInput(audioInput)) {
captureSession.addInput(audioInput)
}

if(captureSession.canAddOutput(movieFileOutput)) {
captureSession.addOutput(movieFileOutput)
}
Expand Down

0 comments on commit bed4308

Please sign in to comment.