Created
October 23, 2020 21:10
-
-
Save rorodriguez116/4e41bfbfc212a61467f6acde4216b6a1 to your computer and use it in GitHub Desktop.
Photo capture function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// MARK: Capture Photo | |
/// - Tag: CapturePhoto | |
public func capturePhoto() { | |
if self.setupResult != .configurationFailed { | |
self.isCameraButtonDisabled = true | |
sessionQueue.async { | |
if let photoOutputConnection = self.photoOutput.connection(with: .video) { | |
photoOutputConnection.videoOrientation = .portrait | |
} | |
var photoSettings = AVCapturePhotoSettings() | |
// Capture HEIF photos when supported. Enable according to user settings and high-resolution photos. | |
if self.photoOutput.availablePhotoCodecTypes.contains(.hevc) { | |
photoSettings = AVCapturePhotoSettings(format: [AVVideoCodecKey: AVVideoCodecType.hevc]) | |
} | |
// Sets the flash option for this capture. | |
if self.videoDeviceInput.device.isFlashAvailable { | |
photoSettings.flashMode = self.flashMode | |
} | |
photoSettings.isHighResolutionPhotoEnabled = true | |
// Sets the preview thumbnail pixel format | |
if !photoSettings.__availablePreviewPhotoPixelFormatTypes.isEmpty { | |
photoSettings.previewPhotoFormat = [kCVPixelBufferPixelFormatTypeKey as String: photoSettings.__availablePreviewPhotoPixelFormatTypes.first!] | |
} | |
photoSettings.photoQualityPrioritization = .quality | |
let photoCaptureProcessor = PhotoCaptureProcessor(with: photoSettings, willCapturePhotoAnimation: { | |
// Tells the UI to flash the screen to signal that SwiftCamera took a photo. | |
DispatchQueue.main.async { | |
self.willCapturePhoto.toggle() | |
} | |
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) { | |
self.willCapturePhoto.toggle() | |
} | |
}, completionHandler: { (photoCaptureProcessor) in | |
// When the capture is complete, remove a reference to the photo capture delegate so it can be deallocated. | |
if let data = photoCaptureProcessor.photoData { | |
self.photo = Photo(originalData: data) | |
print("passing photo") | |
} else { | |
print("No photo data") | |
} | |
self.isCameraButtonDisabled = false | |
self.sessionQueue.async { | |
self.inProgressPhotoCaptureDelegates[photoCaptureProcessor.requestedPhotoSettings.uniqueID] = nil | |
} | |
}, photoProcessingHandler: { animate in | |
// Animates a spinner while photo is processing | |
if animate { | |
self.shouldShowSpinner = true | |
} else { | |
self.shouldShowSpinner = false | |
} | |
}) | |
// The photo output holds a weak reference to the photo capture delegate and stores it in an array to maintain a strong reference. | |
self.inProgressPhotoCaptureDelegates[photoCaptureProcessor.requestedPhotoSettings.uniqueID] = photoCaptureProcessor | |
self.photoOutput.capturePhoto(with: photoSettings, delegate: photoCaptureProcessor) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment