Monday, May 13, 2024
HomeiOS Developmentios - Digital camera Feed isn't working utilizing AVFoundation

ios – Digital camera Feed isn’t working utilizing AVFoundation


On this code, I goal to allow customers to pick out a picture from their cellphone gallery and show it with much less opacity on high of the z-index. The chosen picture ought to seem on high of the consumer’s cellphone digital camera feed, permitting them to see the canvas on which they’re drawing in addition to the low-opacity picture. The app’s function is to allow customers to hint a picture on the canvas whereas concurrently seeing the digital camera feed.
I can see the inexperienced dot above the community bar so I’m certain that the digital camera is getting used however I’m simply unable to see the outcomes.

Please view the display recording from my gadget for higher understanding.
https://rb.gy/m6v6ri

import SwiftUI
import AVFoundation

struct CameraView: View {
    let selectedImage: UIImage
    
    var physique: some View {
        ZStack {
            CameraPreview()
            Picture(uiImage: selectedImage)
                .resizable()
                .aspectRatio(contentMode: .fill)
                .opacity(0.5) // Regulate the opacity as wanted
                .edgesIgnoringSafeArea(.all)
        }
    }
}

struct CameraPreview: UIViewRepresentable {
    func makeUIView(context: Context) -> UIView {
        let cameraPreview = CameraPreviewView()
        return cameraPreview
    }

    func updateUIView(_ uiView: UIView, context: Context) {}
}

class CameraPreviewView: UIView {
    non-public let captureSession = AVCaptureSession()
    
    override init(body: CGRect) {
        tremendous.init(body: body)
        setupCamera()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been applied")
    }
    
    non-public func setupCamera() {
        guard let backCamera = AVCaptureDevice.default(for: .video) else {
            print("Unable to entry digital camera")
            return
        }
        
        do {
            let enter = attempt AVCaptureDeviceInput(gadget: backCamera)
            if captureSession.canAddInput(enter) {
                captureSession.addInput(enter)
                
                let previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
                previewLayer.videoGravity = .resizeAspectFill
                previewLayer.body = bounds
                layer.addSublayer(previewLayer)
                
                captureSession.startRunning()
            }
        } catch {
            print("Error establishing digital camera enter:", error.localizedDescription)
        }
    }
}

I attempted to alter the construction of the code, I’m new to Swift growth and nonetheless studying, so I attempted discovering the answer at ChatGPT and Gemini however I could not discover the problem.

P.S. I’ve declared the permission is data.plist and I’m utilizing my cellphone to run the app, so it has a digital camera to indicate me the feed, in contrast to Simulator iPhones.

Thanks for serving to and your time.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular