Bugs with camera and tabview

Hello everyone, I don't know what to do with my problem. I have a barcode reader in my application which is solved via VisionKit. There are other pages in the bottom bar and they are resolved by TabView. The problem is that when I switch screens, my camera freezes. Does anyone know how to solve this? Thanks for the reply

Replies

Hello,

We would need to see some of your code to be able to assist you. Generally speaking, it sounds like your AVCaptureSession may be stopping when you "switch screens", that could happen for a variety of reasons. For example, if the AVCaptureSession is released when you "switch screens", it will stop. It's also possible that you are stopping it explicitly.

An alternate explanation is that the camera is still running, but for some reason your preview is no longer updating with the latest sample from the camera.

In any case, no conclusion can be made about the source of the issue at this point!

Here is my code:

import VisionKit
import Combine
import AVFoundation
import MessageUI

struct ScannerScreen: View {
    @State private var isShowingScanner = false
    @State private var scannedText = ""
    @State private var codeProcessorMessages: [MessageModel] = []

    private let mailComposeDelegate = MailDelegate()
    
    var body: some View {
        if DataScannerViewController.isSupported && DataScannerViewController.isAvailable {
            ZStack(alignment: .top) {
                DataScannerRepresentable(
                    shouldStartScanning: $isShowingScanner,
                    scannedText: $scannedText,
                    dataToScanFor: [.barcode(symbologies: [.ean8, .ean13, .pdf417])])

                VStack {
                    ForEach(codeProcessorMessages, id: \.self) { message in
                        Text(message.text)
                            .padding()
                            .foregroundColor(.white)
                            .background(message.isGlutenFree ? Color.green : Color.red)
                            .cornerRadius(5)
                    }
                    .padding()
                    .background(Color.black.opacity(0.5).cornerRadius(5))
                    .padding(.bottom, 30)
                }
                VStack{
                    Spacer()
                    HStack {
                        Spacer()
                        
                        Text(scannedText)
                            .padding()
                            .foregroundColor(.black)
                            .background(Color.white
                                .opacity(0.8)
                                .cornerRadius(10))
                            .opacity(scannedText.isEmpty ? 0 : 1)
                        
                        Button(action: {
                            self.presentMailCompose()
                        }) {
                            Text("Nahlásit změnu")
                                .padding()
                                .foregroundColor(.white)
                                .background(Color.blue)
                                .cornerRadius(10)
                                .opacity(scannedText.isEmpty ? 0 : 1)
                        }
                    }
                }
                .padding()
            }
            .onChange(of: scannedText) { newText in
                processCodeWithCodeProcessor(code: newText)
            }
        } else if !DataScannerViewController.isSupported {
            Text("Vypadá to, že zařízení nepodporuje DataScannerViewController")
        } else {
            Text("Zdá se, že váš fotoaparát nemusí být dostupný")
        }
    }

    private func processCodeWithCodeProcessor(code: String) {
        let processor = CodeProcessor()
        codeProcessorMessages = processor.processCode(givenCode: code).map { message in
            let isGlutenFree = message.contains("bezlepková")
            return MessageModel(text: message, isGlutenFree: isGlutenFree)
        }
    }
}

struct ScannerScreen_Previews: PreviewProvider {
    static var previews: some View {
        ScannerScreen()
    }
}

// Struktura pro reprezentaci zprávy s informací o bezlepkovosti
struct MessageModel: Hashable {
    let text: String
    let isGlutenFree: Bool
}