Last active
December 4, 2021 15:21
-
-
Save stinger/0c0e98d8247e4e42c40fd50c7a136e44 to your computer and use it in GitHub Desktop.
An example of using the new AVPhotoCaptureOutput class in iOS 10
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Add the following to the Info.plist | |
// <key>NSCameraUsageDescription</key> | |
// <string>Allow our app to take photos</string> | |
AVCaptureDevice.requestAccess(forMediaType: AVMediaTypeVideo, completionHandler: {[weak self] status in | |
if status { | |
let session = AVCaptureSession() | |
for device in AVCaptureDevice.devices() { | |
if let device = device as? AVCaptureDevice, device.position == AVCaptureDevicePosition.back { | |
self.device = device | |
} | |
} | |
let videoInput = try AVCaptureDeviceInput(device: output.device) | |
session.addInput(videoInput) | |
let imageOutput = AVCapturePhotoOutput() | |
session.addInput(imageInput) | |
if let videoLayer = AVCaptureVideoPreviewLayer(session: session) { | |
videoLayer.frame = previewViewContainer.bounds | |
videoLayer.videoGravity = AVLayerVideoGravityResizeAspectFill | |
self?.previewView.layer.addSublayer(videoLayer) | |
} else { | |
let alert = UIAlertController(title: "Access Requested", message: "We need access to your camera", preferredStyle: .alert) | |
alert.addAction(UIAlertAction(title: "Settings", style: .default, handler: { (action) -> Void in | |
if let url = URL(string:UIApplicationOpenSettingsURLString) { | |
UIApplication.shared.openURL(url) | |
} | |
})) | |
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil)) | |
self?.present(alert, animated: true, completion: nil) | |
} | |
} | |
if let imageOutput = self?.output?.imageOutput as? AVCapturePhotoOutput { | |
let types = imageOutput.availablePhotoCodecTypes | |
if let this = self, types.contains("jpeg") { | |
let settings = AVCapturePhotoSettings(format: [AVVideoCodecKey: "jpeg"]) | |
imageOutput.capturePhoto(with: settings, delegate: this) | |
} | |
} | |
// Protocol conformance | |
@available(iOS 10.0, *) | |
extension UIViewController: AVCapturePhotoCaptureDelegate { | |
func capture(_ captureOutput: AVCapturePhotoOutput, didFinishProcessingPhotoSampleBuffer photoSampleBuffer: CMSampleBuffer?, previewPhotoSampleBuffer: CMSampleBuffer?, resolvedSettings: AVCaptureResolvedPhotoSettings, bracketSettings: AVCaptureBracketedStillImageSettings?, error: Error?) { | |
if let error = error { | |
print(error.localizedDescription) | |
} | |
if let sampleBuffer = photoSampleBuffer, let imageData = AVCapturePhotoOutput.jpegPhotoDataRepresentation(forJPEGSampleBuffer: sampleBuffer, previewPhotoSampleBuffer: previewPhotoSampleBuffer), let image = UIImage(data: imageData) { | |
print("size: \(image.size)") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment