Simple example how to run macOS Camera
https://developer.apple.com/av-foundation/
[8.x.x] | [7.x.x] | |
---|---|---|
Xcode Version | x |
- macOS 10.x
- AVCaptureVideoPreviewLayer
- AVCaptureSession
- AVCaptureDevice
- CameraManagerProtocol
- CameraManager
- CameraManagerDelegate
- Drag and drop the
CameraManager.swift
into your macOS Xcode project.
On viewDidLoad
method, instantiate the CameraManager
object:
do {
cameraManager = try CameraManager(containerView: view)
} catch {
// Cath the error here
}
- Set the CameraManagerDelegate
After you instantiated the camera manager, set it's delegate in this way:
cameraManager.delegate = self
- To start camera, run the following snippet on
viewDidLoad
orviewDidAppear
method:
do {
try cameraManager.startSession()
} catch {
// Cath the error here
}
private var cameraManager: CameraManagerProtocol!
override func viewDidLoad() {
super.viewDidLoad()
do {
cameraManager = try CameraManager(containerView: view)
cameraManager.delegate = self
} catch {
// Cath the error here
print(error.localizedDescription)
}
}
override var representedObject: Any? {
didSet {
// Update the view, if already loaded.
}
}
override func viewDidAppear() {
super.viewDidAppear()
do {
try cameraManager.startSession()
} catch {
// Cath the error here
print(error.localizedDescription)
}
}
override func viewDidDisappear() {
super.viewDidDisappear()
do {
try cameraManager.stopSession()
} catch {
// Cath the error here
print(error.localizedDescription)
}
}
extension YourViewController: CameraManagerDelegate {
func cameraManager(_ output: CameraCaptureOutput, didOutput sampleBuffer: CameraSampleBuffer,
from connection: CameraCaptureConnection) {
print(Date())
}
}
No Bugs
- Fork the project.
- Create a branch for your new feature.
- Write tests.
- Write code to make the tests pass.
- Submit a pull request.
All pull requests are welcome !