Skip to content

Configuring MCamera

Tomasz K. edited this page Dec 12, 2024 · 29 revisions

We mnie jest dwu ludzi, jeden zupełnie rozsądny, drugi wariat. Który zaś zwycięży?

Configuring MCamera

Overview

There are numerous methods that can be used to modify the initial state of the camera. On this page we'll discuss them all and then I'll show you how to use them correctly.

Available Methods

  • func setCameraOutputType(_ cameraOutputType: CameraOutputType) -> Self

    • Changes the initial camera output type.
  • func setCameraPosition(_ cameraPosition: CameraPosition) -> Self

    • Changes the initial camera position.
    • If the selected camera position is not available, the camera will not be changed.
  • func setAudioAvailability(_ isAvailable: Bool) -> Self

    • Definies whether the audio source is available.
    • If disabled, the camera will not record audio, and will not ask for permission to access the microphone.
  • func setZoomFactor(_ zoomFactor: CGFloat) -> Self

    • Changes the initial camera zoom level.
    • If the zoom factor is out of bounds, it will be set to the closest available value.
  • func setFlashMode(_ flashMode: CameraFlashMode) -> Self

    • Changes the initial camera flash mode.
    • If the selected flash mode is not available, the flash mode will not be changed.
  • func setLightMode(_ lightMode: CameraLightMode) -> Self

    • Changes the initial light (torch / flashlight) mode.
    • If the selected light mode is not available, the light mode will not be changed.
  • func setResolution(_ resolution: AVCaptureSession.Preset) -> Self

    • Changes the initial camera resolution.
    • Changing the resolution may affect the maximum frame rate that can be set.
  • func setFrameRate(_ frameRate: Int32) -> Self

    • Changes the initial camera frame rate.
    • Depending on the resolution of the camera and the current specifications of the device, there are some restrictions on the frame rate that can be set. If you set a frame rate that exceeds the camera's capabilities, the library will automatically set the closest possible value and show you which value has been set.
  • func setCameraExposureDuration(_ duration: CMTime) -> Self

    • Changes the initial camera exposure duration.
    • If the exposure duration is out of bounds, it will be set to the closest available value.
  • func setCameraTargetBias(_ targetBias: Float) -> Self

    • Changes the initial camera target bias.
    • If the target bias is out of bounds, it will be set to the closest available value.
  • func setCameraISO(_ iso: Float) -> Self

    • Changes the initial camera ISO.
    • If the ISO is out of bounds, it will be set to the closest available value.
  • func setCameraExposureMode(_ exposureMode: AVCaptureDevice.ExposureMode) -> Self

    • Changes the initial camera exposure mode.
    • If the exposure mode is not supported, the exposure mode will not be changed.
  • func setCameraHDRMode(_ hdrMode: CameraHDRMode) -> Self

    • Changes the initial camera HDR mode.
  • func setCameraFilters(_ filters: [CIFilter]) -> Self

    • Changes the initial camera filters.
    • Setting multiple filters simultaneously can affect the performance of the camera.
  • func setMirrorOutput(_ shouldMirror: Bool) -> Self

    • Changes the initial mirror output setting.
  • func setGridVisibility(_ shouldShowGrid: Bool) -> Self

    • Changes the initial grid visibility setting.
  • func setFocusImage(_ image: UIImage) -> Self

    • Changes the shape of the focus indicator visible when touching anywhere on the camera screen.
  • func setFocusImageColor(_ color: UIColor) -> Self

    • Changes the color of the focus indicator visible when touching anywhere on the camera screen.
  • func setFocusImageSize(_ size: CGFloat) -> Self

    • Changes the size of the focus indicator visible when touching anywhere on the camera.
  • func setCameraScreen(_ builder: @escaping CameraScreenBuilder) -> Self

  • func setCapturedMediaScreen(_ builder: CapturedMediaScreenBuilder?) -> Self

  • func setErrorScreen(_ builder: @escaping ErrorScreenBuilder) -> Self

  • func lockCameraInPortraitOrientation(_ appDelegate: MApplicationDelegate.Type) -> Self

    • Locks the screen in portrait mode when the Camera Screen is active.
    • For further information, see Blocking camera rotation section of this page.
  • func setCloseMCameraAction(_ action: @escaping () -> ()) -> Self

    • Indicates how the MCamera can be closed.
    • For further information, see Closing MCamera section of this page.
  • func onImageCaptured(_ action: @escaping (UIImage, MCamera.Controller) -> ()) -> Self

    • Defines action that is called when an image is captured.
    • The action is called immediately if Captured Media Screen is nil, otherwise after the user accepts the photo.
  • func onVideoCaptured(_ action: @escaping (URL, MCamera.Controller) -> ()) -> Self

    • Defines action that is called when a video is captured.
    • The action is called immediately if Captured Media Screen is nil, otherwise after the user accepts the video.

Using methods

To call the selected method(s), simply add them to MCamera. Do not forget to insert startSession() at the end, otherwise the MCamera will not start!

struct ContentView: View {
    var body: some View {
        (...)
        MCamera()
            .setResolution(.hd4K3840x2160)
            .setFrameRate(27)
            .setLightMode(.on)
            .setFlashMode(.auto)
            .setZoomFactor(1.3)
            .setCameraFilters([.init(name: "CISepiaTone")!])
            .startSession()
        (...)
    }
}

Blocking camera rotation

Blocking the camera from rotating is quite simple; create an AppDelegate that conforms to MApplicationDelegate, place it in the @main struct of your app, and you're done!

@main struct App_Main: App {
    @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate

    var body: some Scene {
        WindowGroup(content: ContentView.init)
    }
}

// MARK: App Delegate
class AppDelegate: NSObject, MApplicationDelegate {
    static var orientationLock = UIInterfaceOrientationMask.all

    func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask { AppDelegate.orientationLock }
}

// MARK: Content View
struct ContentView: View {
    var body: some View {
        MCamera()
            .lockCameraInPortraitOrientation(AppDelegate.self)

            // MUST BE CALLED!
            .startSession()
    }
}

Closing MCamera

The setCloseMCameraAction() function defines an action that closes the MCamera. In the default Camera Screen configuration, this is used to handle the Close button.

struct ContentView: View {
    @State private var isSheetPresented: Bool = false


    var body: some View {
        Button(action: { isSheetPresented = true }) {
            Text("Click me!")
        }
        .fullScreenCover(isPresented: $isSheetPresented) {
            MCamera()
                .setResolution(.hd1920x1080)
                .setCloseMCameraAction { isSheetPresented = false }

                // MUST BE CALLED!
                .startSession()
        }
    }
}

See also