2018-08-16 11:18:27 +02:00
|
|
|
|
/*
|
|
|
|
|
See LICENSE.txt for this sample’s licensing information.
|
|
|
|
|
|
|
|
|
|
Abstract:
|
|
|
|
|
Photo capture delegate.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import AVFoundation
|
|
|
|
|
import Photos
|
2020-05-16 11:21:55 +02:00
|
|
|
|
import UIKit
|
2018-08-16 11:18:27 +02:00
|
|
|
|
|
|
|
|
|
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 {
|
2020-05-16 11:21:55 +02:00
|
|
|
|
log("PhotoCaptureHandler: \(error!)")
|
2018-08-16 11:18:27 +02:00
|
|
|
|
delegate?.didCapture(nil)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-28 15:54:13 +02:00
|
|
|
|
guard let cgImage = photo.cgImageRepresentation() else {
|
2020-05-16 11:21:55 +02:00
|
|
|
|
log("PhotoCaptureHandler: No image captured")
|
2018-08-16 11:18:27 +02:00
|
|
|
|
delegate?.didCapture(nil)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let image = UIImage(cgImage: cgImage, scale: 1.0, orientation: .right)
|
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
|
self.delegate?.didCapture(image)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-05-16 11:21:55 +02:00
|
|
|
|
|
|
|
|
|
extension PhotoCaptureHandler: Logger {
|
|
|
|
|
|
|
|
|
|
}
|