Wrapper around AVFoundation camera. Works with several data outputs, such as metadata, video data, still image capture.
- iOS 15.0+
- Xcode 16.0+
- Swift 5.0+
To integrate VideoCamera into your Xcode project with CocoaPods, specify it in your Podfile
:
pod 'Shakuro.VideoCamera'
Then, run the following command:
$ pod install
If you prefer not to use CocoaPods, you can integrate Shakuro.VideoCamera simply by copying it to your project.
Each VideoCamera
must be provided with VideoCameraConfiguration
structure. Go though it's extensive array of settings. For a simple setup of back-facing camera, that provides only video data preview without ability to get still images:
private var camera: VideoCamera?
override func viewDidLoad() {
super.viewDidLoad()
// ...
var cameraConfig = VideoCameraConfiguration()
cameraConfig.cameraDelegate = self
cameraConfig.captureSessionPreset = .high
cameraConfig.capturePhotoEnabled = false
cameraConfig.videoFeedDelegate = self
cameraConfig.simulatedImage = UIImage(named: "card_backside.png")?.cgImage
let videoCamera = VideoCameraFactory.createCamera(configuration: cameraConfig)
camera = videoCamera
// ...
}
To display camera preview, one of the simplest ways is to prepare container view in storyboard and than add camera's preview to it:
@IBOutlet private var cameraPreviewContainerView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
// setup camera (see above)
let preview = videoCamera.previewView
preview.translatesAutoresizingMaskIntoConstraints = true
preview.autoresizingMask = [UIViewAutoresizing.flexibleHeight, UIViewAutoresizing.flexibleWidth]
preview.frame = cameraPreviewContainerView.bounds
cameraPreviewContainerView.insertSubview(preview, at: 0)
// ...
}
Changes of camera's properties are observed via delegate:
extension MyViewController: VideoCameraDelegate {
func videoCamera(_ videoCamera: VideoCamera, error: Error) {
// display/process error
}
func videoCameraInitialized(_ videoCamera: VideoCamera, errors: [VideoCameraError]) {
// update UI - such as flash button (with actual state of camera)
}
func videoCamera(_ videoCamera: VideoCamera, flashModeForPhotoDidChanged newValue: AVCaptureDevice.FlashMode) {
// update flash button
}
// ... other delegate functions
}
Have a look at the VideoCamera_Example
Shakuro.VideoCamera is released under the MIT license. See LICENSE for details.
Star this tool if you like it, it will help us grow and add new useful things. Feel free to reach out and hire our team to develop a mobile or web project for you.