CameraView icon indicating copy to clipboard operation
CameraView copied to clipboard

[BUG] Subsequent image captures result in crash when trying to access captured image

Open frozenbubble opened this issue 8 months ago • 1 comments

Prerequisites

  • [/] I checked the documentation and found no answer
  • [/] I checked to make sure that this issue has not already been filed

Expected Behavior

When taking multiple subsequent images, onImageCaptured gets called and i can access the captured image without any issues.

Current Behavior

With the following code:

struct CameraSandbox: View {
    @State var captureImage: UIImage?
    @State var displayImage: Bool = false
    
    var body: some View {
        ZStack {
            MCamera()
                .setAudioAvailability(false)
                .setErrorScreen(CustomErrorScreen.init)
                .setCapturedMediaScreen(nil) // Disable captured media screen
                .setCameraScreen(CustomCameraScreen.init)
                .onImageCaptured { (image, controller) in
                    captureImage = image
                    controller.reopenCameraScreen()
                }
                .startSession() // Start the camera session
        }
        .onChange(of: captureImage) {
            displayImage = captureImage != nil
        }
        .sheet(isPresented: $displayImage) {
            if let captureImage {
                Image(uiImage: captureImage)
                    .resizable()
                    .scaledToFit()
            }
        }
    }
}

The first attempt results in the image correctly displayed in the sheet, and the second attempt consistently results in a crash at MijickCamera/CameraManager+PhotoOutput.swift:45:

settings.flashMode = parent.attributes.flashMode.toDeviceFlashMode()

With parent being nil

If i comment out captureImage = image in my onImageCaptured handler, camera sound and animation plays without crash.

Steps to Reproduce

Please provide detailed steps for reproducing the issue.

  1. Create a camera screen with the captured media screen disabled and custom camera screen
  2. Specify onImageCaptured and try to store the result image
  3. Take subsequent images from the camera screen
  4. Observe crash

Code Sample

See above

Screenshots

N/A

Context

Name Version
SDK e.g. 3.0.2
Xcode Version 16.2 (16C5032a)
Operating System e.g. iOS 18.3.2
Device e.g. iPhone XS

frozenbubble avatar Apr 15 '25 12:04 frozenbubble

I hit this bug as well. A (non-optimal) workaround is to cause the MCamera() to be reinstantiated after picture taking like this :

@State var sequence: Int = 0

...

MCamera()
  .onImageCaptured({ _,_ in 
    sequence += 1
    ...
  })
  .id(sequence)

hinathan avatar Sep 02 '25 16:09 hinathan