52 lines
1.3 KiB
Swift
52 lines
1.3 KiB
Swift
|
/*
|
|||
|
See LICENSE.txt for this sample’s 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)
|
|||
|
}
|
|||
|
}
|
|||
|
}
|