Caps-iOS/CapCollector/Capture/PhotoCaptureHandler.swift

52 lines
1.3 KiB
Swift
Raw Normal View History

2018-08-16 11:18:27 +02:00
/*
See LICENSE.txt for this samples licensing information.
Abstract:
Photo capture delegate.
*/
import AVFoundation
import Photos
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 {
print("PhotoCaptureHandler: \(error!)")
delegate?.didCapture(nil)
return
}
guard let cgImage = photo.cgImageRepresentation()?.takeUnretainedValue() else {
print("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)
}
}
}