On this code, I intention to allow customers to pick out a picture from their telephone gallery and show it with much less opacity on prime of the z-index. The chosen picture ought to seem on prime of the consumer’s telephone digicam feed, permitting them to see the canvas on which they’re drawing in addition to the low-opacity picture. The app’s objective is to allow customers to hint a picture on the canvas whereas concurrently seeing the digicam feed.
I can see the inexperienced dot above the community bar so I’m positive that the digicam is getting used however I’m simply unable to see the outcomes.
Please view the display screen 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 digicam")
return
}
do {
let enter = strive 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 digicam enter:", error.localizedDescription)
}
}
}
I attempted to vary 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 information.plist and I’m utilizing my telephone to run the app, so it has a digicam to indicate me the feed, not like Simulator iPhones.
Thanks for serving to and your time.