CameraKage 是一个完全可定制、纯 Swift、即插即用的相机视图。
使用模块的共享实例,您可以处理相机和麦克风权限,并创建您需要的相机视图类型。(照片相机、视频相机或能够同时进行照片拍摄和视频录制的完整相机)
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
}
}
要接收来自相机的通知,您必须注册为侦听器并实现特定相机的委托协议。
cameraView.registerDelegate(self)
设置完成后,最后一步是调用相机的 startCamera()
方法
cameraView.startCamera()
完成这些步骤后,您应该启动并运行相机,剩下的就是调用 capturePhoto 或 startVideoRecording 方法。
CameraKage 具有创建 AR 相机的选项,该相机能够将面具加载到用户的脸上并使用它们捕获内容。要使用 AR 相机,您首先必须将面具的 3D 模型导入到应用程序的 bundle 中。
有了 3D 模型后,您可以开始创建 ARCameraView 并将其添加到您的 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()
现在有了基本设置,您可以开始捕获内容和加载面具。 要加载面具,您只需调用 loadARMask 方法
// don't use . in front of the extension
arCameraView.loadARMask(name: "maskName", fileType: "maskExtension")
CameraKage 提供了许多关于相机和正在进行的相机会话的有用通知
/**
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()
并且还有特定于相机的委托方法
/**
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()
https://github.com/andreilob/CameraKage.git
欢迎任何贡献,但在任何 pull request 之前,请先发起讨论。
如有任何联系,您可以在 Linkedin 上找到我。 如果您发现问题,请打开一个 ticket。