CameraShot.swift
1.05 KB
//
// CameraShot.swift
// ALCameraViewController
//
// Created by Alex Littlejohn on 2015/06/17.
// Copyright (c) 2015 zero. All rights reserved.
//
import UIKit
import AVFoundation
public typealias CameraShotCompletion = (UIImage?) -> Void
public func takePhoto(_ stillImageOutput: AVCaptureStillImageOutput, videoOrientation: AVCaptureVideoOrientation, cropSize: CGSize, completion: @escaping CameraShotCompletion) {
guard let videoConnection: AVCaptureConnection = stillImageOutput.connection(withMediaType: AVMediaTypeVideo) else {
completion(nil)
return
}
videoConnection.videoOrientation = videoOrientation
stillImageOutput.captureStillImageAsynchronously(from: videoConnection, completionHandler: { buffer, error in
guard let buffer = buffer,
let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(buffer),
let image = UIImage(data: imageData) else {
completion(nil)
return
}
completion(image)
})
}