CameraKage is a fully customizable, pure Swift, plug-and-play camera view.
- Fully customizable and composable camera views.
- Photo and video capture.
- AR camera capable of loading face masks and capturing content with them.
- Metadata camera scanner. (QR, barcodes, etc.)
- Premission handling.
- Video and Photo compression and resizing.
- Delegate notifications.
- Camera flipping.
- Adjustments for exposure and focus of the camera.
- Capture session error and interruptions notifiers.
- Flash usage for both photo and video.
Using the shared instance of the module you can handle camera and microphone permissions and create the type of camera view you would need. (Photo Camera, Video Camera or a full camera capable of both photo capture and video recordings)
let cameraPermissionGranted = await CameraKage.shared.requestCameraPermission()
let microphonePermissionGranted = await CameraKage.shared.requestMicrophonePermission()
if cameraPermissionGranted, microphonePermissionGranted {
let cameraCreationResult = CameraKage.shared.createCameraView(with: CameraComponentParsedOptions([
.cameraDevice(.backUltraWideCamera),
.flipCameraDevice(.frontCamera),
.maxVideoDuration(20.0),
.pinchToZoomEnabled(true)
]))
switch cameraCreationResult {
case .success(let cameraView):
// Add camera to your view
case .failure(let error):
// Handle error that might occur
}
}
To receive notifications from the camera, you have to register as a listener and implement the delegate protocol of the specific camera.
cameraView.registerDelegate(self)
After the setup, the last thing to be done is to call the camera startCamera()
method
cameraView.startCamera()
With these steps you should have you camera up an running, what's left is just to call the capturePhoto or startVideoRecording methods.
CameraKage has the option to create an AR camera capable of loading masks onto the face of users and capture content with them. To use the AR camera you firstly have to import 3D models of the masks into the bundle of your application.
After you have the 3D models you can start creating the ARCameraView and add it to your ViewController.
import CameraKage
let arCameraView = CameraKage.shared.createARCameraView()
// Register the delegate to receive info from the camera.
arCameraView.registerDelegate(self)
// Add the camera to the view and constraint it.
view.addSubview(arCameraView)
// Then just start the camera:
arCameraView.startCamera()
Now having the basic setup, you can start capturing content and loading masks. To load masks you just have to call the loadARMask method:
// don't use . in front of the extension
arCameraView.loadARMask(name: "maskName", fileType: "maskExtension")
CameraKage provides a handful of useful notifications regarding the camera and the on-going camera session:
/**
Called when a pinch to zoom action happened on the camera.
- parameter scale: The current zoom scale reported by the pinch gesture.
- parameter maxScale: The maximum zoom scale of the camera.
*/
func cameraDidZoom(atScale scale: CGFloat, outOfMaximumScale maxScale: CGFloat)
/**
Called when the camera encountered an error.
- parameter error: The error that was encountered.
*/
func cameraDidEncounterError(error: CameraError)
/**
Called when the camera session was interrupted. This can happen from various reason but most common ones would be phone calls while using the camera, other apps taking control over the phone camera or app moving to background.
- parameter reason: The reason for the session interruption.
- important: When this is called, the camera will freezee so some UI overlay might be necessary on the client side.
*/
func cameraDidReceiveSessionInterruption(withReason reason: SessionInterruptionReason)
/**
Called when the camera session interruption has ended. When this is called the camera will resume working.
*/
func cameraDidFinishSessionInterruption()
/**
Called when the camera session was started and the actual camera will be visible on screen.
*/
func cameraDidStartCameraSession()
/**
Called when the camera session has stopped.
*/
func cameraDidStopCameraSession()
/**
Called when the instance of AVCaptureDevice has detected a substantial change to the video subject area. This notification is only sent if you first set monitorSubjectAreaChange to `true` in the `focus()` camera method.
*/
func cameraDidChangeDeviceAreaOfInterest()
And also there are the camera specific delegate methods:
/**
Called when the camera has outputted a photo.
- parameter data: The data representation of the photo.
*/
func cameraDidCapturePhoto(withData data: Data)
/**
Called when the camera has started a video recording.
- parameter url: The URL file location where the video is being recorded.
*/
func cameraDidStartVideoRecording(atFileURL url: URL)
/**
Called when the camera has outputted a video recording.
- parameter url: The URL of the video file location.
*/
func cameraDidFinishVideoRecording(atFileURL url: URL)
/**
Called when there was a successful metadata scan for the specified metadata types.
- parameter metadata: An array representing all the metadata that was detected.
*/
func cameraDidScanMetadataInfo(metadata: [MetadataScanOutput])
/**
Called when the AR camera has outputted a photo.
- parameter data: The data representation of the photo.
*/
func arCamera(didCapturePhotoWithData data: Data)
/**
Called when the AR camera has started a video recording.
- parameter url: The URL file location where the video is being recorded.
*/
func arCamera(didBeginRecordingVideoAtURL url: URL)
/**
Called when the camera has outputted a video recording.
- parameter url: The URL of the video file location.
*/
func arCamera(didRecordVideoAtURL url: URL)
/**
Called when the AR camera encountered an error.
- parameter error: The error that was encountered.
*/
func arCamera(didFailWithError error: ARCameraError)
/**
Called when the camera session was interrupted. This can happen from various reason but most common ones would be phone calls while using the camera, other apps taking control over the phone camera or app moving to background.
- important: When this is called, the camera will freezee so some UI overlay might be necessary on the client side.
*/
func arCameraWasInterrupted()
/**
Called when the camera session interruption has ended. When this is called the camera will resume working.
*/
func arCameraInterruptionEnded()
- iOS 15.0+
- Swift 5.6+
- File > Swift Packages > Add Package Dependency
- Add
https://github.com/andreilob/CameraKage.git
- Select "Up to Next Major" with "2.0.0"
Any contribution is welcome but before any pull request, please open a discussion first.
For any contact you can find me on Linkedin. If you find an issue, open a ticket.