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