Caps-iOS/CapCollector/Capture/PhotoCaptureHandler.swift
2020-05-16 11:21:55 +02:00

57 lines
1.4 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
See LICENSE.txt for this samples licensing information.
Abstract:
Photo capture delegate.
*/
import AVFoundation
import Photos
import UIKit
protocol PhotoCaptureHandlerDelegate {
func didCapture(_ image: UIImage?)
}
class PhotoCaptureHandler: NSObject {
var delegate: PhotoCaptureHandlerDelegate?
var photoSettings: AVCapturePhotoSettings {
let photoSettings = AVCapturePhotoSettings()
photoSettings.flashMode = .off
return photoSettings
}
}
extension PhotoCaptureHandler: AVCapturePhotoCaptureDelegate {
/*
This extension includes all the delegate callbacks for AVCapturePhotoCaptureDelegate protocol
*/
func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
guard error == nil else {
log("PhotoCaptureHandler: \(error!)")
delegate?.didCapture(nil)
return
}
guard let cgImage = photo.cgImageRepresentation()?.takeUnretainedValue() else {
log("PhotoCaptureHandler: No image captured")
delegate?.didCapture(nil)
return
}
let image = UIImage(cgImage: cgImage, scale: 1.0, orientation: .right)
DispatchQueue.main.async {
self.delegate?.didCapture(image)
}
}
}
extension PhotoCaptureHandler: Logger {
}