1 year ago

#374120

test-img

aadityaaa

How to convert a complex UIKit View into a SwiftUI View?

there's a lot of code in here but please be patient I will try to explain my question

so i am trying to convert Apple's AvDepthCapture project which is in UIKit as a SwiftUI project, so i copied all the supporting files, i will attach the image for the same below, the shaders folder contain the metal files for applying filters
enter image description here

so I created a class same as the cameraviewcontroller and added all the properties as follows -

class CameraViewController: UIViewController, AVCapturePhotoCaptureDelegate, AVCaptureVideoDataOutputSampleBufferDelegate, AVCaptureDepthDataOutputDelegate, AVCaptureDataOutputSynchronizerDelegate, ObservableObject {
    
  

    
    private var previewView: PreviewMetalView!
    

} now when i run my app on my device it asks for camera permission but then shows a white screen instead of opening the camera and the PreviewMetalView file that i copied earlier directly from the original UIKit project, the declaration is causing error

private var previewView: PreviewMetalView!

this line, it quits my app and says unexpectedly found nil when unwrapping an optional value but the code is the same as that of the UIKit project and UIKit project works perfectly fine on my device then why is it throwing this error!?, please help i can provide both the UIKit project and my SwiftUI project file if you want to see i really can't figure it out

there's a lot of code but this specific line is throwing error, I declared a type of PreviewMetalView but now PreviewMetalView has an initialiser which takes in a type NSCoder so what should I write there ?? like I would be declaring my variable as var previewView = PreviewMetalView(coder: NSCoder) what to put in place of NSCoder ?

when I opened the original UIKit project the previewView is defined as an IBOutlet to a Container View which is then linked to the CameraViewController but I cannot find where is it initialised please help!

swiftui

uikit

metal

depth

avkit

0 Answers

Your Answer

Accepted video resources