[ad_1]
My problem principally pertains to points regarding AVFoundations, UIKit, and SwiftUI. Any assist, pointers, and/or recommendation can be most appreciated
My Drawback
Quick and candy abstract of the objective: I’ve an app I’m constructing with many various pages. One among these pages is presupposed to show a Customized Digicam view with some easy UI (principally a file/cease button). The Customized Digicam view has to show the inputs from each the iPhone’s back and front cameras.
I can get a customized digicam view with ui working, though ONLY with a single digicam and utilizing AVCaptureSession. I’ve been at it for the previous week attempting to show two previews of the back and front digicam however run into fixed crashes and black screens.
I’ve gone over Apple’s AVMultiCamSessionPIP undertaking from a pair years again. However it’s a single view app that makes use of IBOutlets, UIKit, View Controllers, and Storyboards. My app is in MVVM structure and makes use of principally SwiftUI (with the one UIKit being from UIViewRepresentable to attach UIKit to my swiftUi) so it gives perception into organising the session, however not displaying.
The Answer
A technique to show a preview the back and front cameras in an embedded view inside swiftUI.
My Present Implementation
Listed here are the related bits of code that I’m utilizing. Largest be aware is that the index is meant to used to pick what digicam you wish to preview. So you must be capable to add two to the view for instance with totally different values
If extra is required I can elaborate:
Drive.swift – That is the web page customers navigate to in-app
import SwiftUI
struct DriveView: View {
@EnvironmentObject var cameraModel: CameraViewModel
@StateObject var driveViewModel = DriveViewModel()
@ObservedObject var mainMenuViewModel: MainMenuViewModel
var physique: some View {
ZStack(alignment: .backside) {
VStack {
CameraView(session: cameraModel.captureSession, index: 0)
.environmentObject(cameraModel)
}
// // Digicam View
// CameraView()
// .environmentObject(cameraModel)
// .padding(.backside)
ZStack {
swap driveViewModel.isDriving {
case false:
// Predrive UI
// Occasion Button
VStack {
Spacer()
HStack(alignment: .heart, spacing: 0) {
Button(motion: {
mainMenuViewModel.pageState = 2
}, label: {
VStack {
Picture(systemName: "backward.body.fill")
.resizable()
.renderingMode(.template)
.padding(12)
.foregroundColor(.white)
.body(width: 40, peak: 40)
Textual content("Again")
.foregroundColor(.white)
.font(.system(measurement: 18))
.fontWeight(.semibold)
}
})
.padding(.main,40)
Spacer()
Button(motion: {
driveViewModel.isDriving = true
}, label: {
VStack {
Picture(systemName: "circle.grid.3x3.circle.fill")
.resizable()
.renderingMode(.template)
.padding(12)
.foregroundColor(.white)
.body(width: 90, peak: 90)
Textual content("Begin Drive")
.foregroundColor(.white)
.font(.title2)
.fontWeight(.semibold)
}
})
Spacer()
Spacer()
}
}
.body(maxWidth:.infinity,alignment: .heart)
// Button {
//
// } label: {
// Picture(systemName: "cease")
// .resizable()
// .renderingMode(.template)
// .aspectRatio(contentMode: .match)
// .foregroundColor(.white)
// .padding(12)
// .body(width: 60, peak: 60)
// .background(
// Circle()
// .fill(.black)
// )
// .padding(6)
// .background(
// Circle()
// .fill(.white)
// )
// }
// .body(maxWidth:.infinity, alignment:.trailing)
// .padding(.trailing, 60)
// Throughout drive UI
case true:
VStack {
Spacer()
HStack(alignment: .heart, spacing: 0) {
Spacer()
// Cease Drive Button
Button(motion: {
// Summary: Might want to delete all film recordsdata it created
driveViewModel.isDriving = false
}, label: {
VStack {
Picture(systemName: "nosign")
.resizable()
.renderingMode(.template)
.padding(12)
.foregroundColor(.white)
.body(width: 70, peak: 70)
Textual content("Finish Drive")
.foregroundColor(.white)
.font(.title2)
.fontWeight(.semibold)
}
})
Spacer()
// Create Occasion Button
Button(motion: {
// Summary: Might want to compile final 2 session, sew them and save them, then cease
driveViewModel.isDriving = false
}, label: {
VStack {
Picture(systemName: "pin.sq..fill")
.resizable()
.renderingMode(.template)
.padding(12)
.foregroundColor(.white)
.body(width: 70, peak: 70)
Textual content("Mark Occasion")
.foregroundColor(.white)
.font(.title2)
.fontWeight(.semibold)
}
})
Spacer()
// Placeholder Temp Button
// Button(motion: {
// mainMenuViewModel.pageState = 0
// }, label: {
// VStack {
// Picture(systemName: "backward.body.fill")
// .resizable()
// .renderingMode(.template)
// .padding(12)
// .foregroundColor(.white)
// .body(width: 40, peak: 40)
//
// Textual content("Again")
// .foregroundColor(.white)
// .font(.system(measurement: 18))
// .fontWeight(.semibold)
// }
// })
// .padding(.main,40)
//
// Spacer()
}
}
.body(maxWidth:.infinity,alignment: .heart)
}
}
.body(maxHeight:.infinity,alignment: .backside)
.padding(.backside,10)
.padding(.backside,30)
}
.body(maxWidth: .infinity)
.background(.black)
}
}
Cameraview.swift
import SwiftUI
import AVFoundation
struct CameraView: View {
@EnvironmentObject var cameraModel: CameraViewModel
var session: AVCaptureMultiCamSession? = nil
var index: Int?
var shouldExpand: Bool = true
var physique: some View {
if index != nil {
return AnyView(LayerView(session: cameraModel.captureSession!, index: index!)
.environmentObject(cameraModel)
.body(minWidth: 0,
idealWidth: .infinity,
maxWidth: .infinity,
minHeight: 0,
idealHeight: (shouldExpand) ? .infinity : 0,
maxHeight: (shouldExpand) ? .infinity : 0))
}
print("ought to increase (shouldExpand)")
return AnyView(Rectangle().fill(.pink).body(minWidth: 0, idealWidth: (shouldExpand) ? .infinity : 0, maxWidth: (shouldExpand) ? .infinity : 0, minHeight: 0, idealHeight: (shouldExpand) ? .infinity : 0, maxHeight: (shouldExpand) ? .infinity : 0))
}
}
PreviewController.swift
import Basis
import AVFoundation
import UIKit
import SwiftUI
// setting view for preview...
struct LayerView: UIViewRepresentable {
@EnvironmentObject var cameraModel : CameraViewModel
var session: AVCaptureMultiCamSession
var index: Int
func makeUIView(context: UIViewRepresentableContext<LayerView>) -> PreviewView {
return PreviewView(session: cameraModel.captureSession!, index: index)
}
func updateUIView(_ uiView: PreviewView, context: UIViewRepresentableContext<LayerView>) {
}
typealias UIViewType = PreviewView
}
class PreviewView: UIView {
@EnvironmentObject var cameraModel : CameraViewModel
let session: AVCaptureMultiCamSession
let index: Int
var videoPreviewLayer: AVCaptureVideoPreviewLayer {
return layer as! AVCaptureVideoPreviewLayer
}
init(session:AVCaptureMultiCamSession, index: Int){
self.session = session
self.index = index
tremendous.init(body: .zero)
CameraViewModel.addConnection(session: cameraModel.captureSession!, layer: videoPreviewLayer, digicam: index)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been carried out")
}
override class var layerClass: AnyClass {
return AVCaptureVideoPreviewLayer.self
}
}
CameraViewModel.swift
import Basis
import AVFoundation
import Mix
import SwiftUI
//MARK: Digicam View Mannequin
class CameraViewModel: ObservableObject {
non-public var session = AVCaptureMultiCamSession()
var captureSession: AVCaptureMultiCamSession? {
return self.session
}
@Printed var alert = false
@Printed var output = AVCaptureMovieFileOutput()
@Printed var preview : AVCaptureVideoPreviewLayer!
init() {
checkPermission()
}
func checkPermission(){
// first checking permission...
swap AVCaptureDevice.authorizationStatus(for: .video) {
case .approved:
setUp()
return
// Setting Up Session
case .notDetermined:
// retusting for permission....
AVCaptureDevice.requestAccess(for: .video) { (standing) in
if standing{
self.setUp()
}
}
case .denied:
self.alert.toggle()
return
default:
return
}
}
func setUp(){
self.session = AVCaptureMultiCamSession()
self.session.beginConfiguration()
let frontDevice = AVCaptureDevice.default(.builtInWideAngleCamera, for: .video, place: .entrance)!
let backDevice = AVCaptureDevice.default(.builtInWideAngleCamera, for: .video, place: .again)!
let enter = attempt! AVCaptureDeviceInput(gadget: frontDevice)
let input2 = attempt! AVCaptureDeviceInput(gadget: backDevice)
self.session.addInputWithNoConnections(enter)
self.session.addInputWithNoConnections(input2)
let output = AVCaptureVideoDataOutput()
//session.sessionPreset = AVCaptureSession.Preset.hd4K3840x2160
UIDevice.present.beginGeneratingDeviceOrientationNotifications()
self.session.addOutput(output)
// let cameraDevice = AVCaptureDevice.default(.builtInWideAngleCamera, for: .video, place: .again)
// let cameraInput = attempt AVCaptureDeviceInput(gadget: cameraDevice!)
// let audioDevice = AVCaptureDevice.default(for: .audio)
// let audioInput = attempt AVCaptureDeviceInput(gadget: audioDevice!)
//
// if self.session.canAddInput(cameraInput) && self.session.canAddInput(audioInput){
// self.session.addInput(cameraInput)
// self.session.addInput(audioInput)
// }
//
// if self.session.canAddOutput(self.output){
// self.session.addOutput(self.output)
// }
self.session.commitConfiguration()
}
static func addConnection(session: AVCaptureMultiCamSession, layer: AVCaptureVideoPreviewLayer, digicam: Int){
layer.videoGravity = .resizeAspectFill
layer.setSessionWithNoConnection(session)
print(session.inputs)
let videoPort = session.inputs[camera].ports.first!
let connection = MyConnection(inputPort: videoPort, videoPreviewLayer: layer)
session.addConnection(connection)
session.commitConfiguration()
}
}
class MyConnection : AVCaptureConnection {
override var videoMaxScaleAndCropFactor: CGFloat {
return 2.0
}
}
Once more, any and all assist and recommendation is appreciated. I’m prepared to burn this present type of the code if wanted. However I really feel like I’m working my head right into a brick wall and there is not a lot in the way in which of sources for figuring it out.
[ad_2]
