[ad_1]
I’m engaged on a SwiftUI app that shows an AVCaptureVideoPreviewLayer and in addition implements the AVCaptureVideoDataOutputSampleBufferDelegate protocol to carry out some customized logic in captureOutput(_: didOutput: from:). The customized logic was working as anticipated and updating my view as anticipated till I applied the video preview layer.
Now, solely the video preview layer is up to date inside the view. Each the video preview layer and the replace to the printed variable happen inside a name to DispatchQueue.fundamental.async. Is that this acceptable?
I even have a suspicion that I’ll must implement some logic inside the updateUIViewController(_: context:) operate inside the UIViewControllerRepresentable struct I’m utilizing to show the video peview layer for my part. The docs supplied for this operate aren’t very useful, can anybody present any recommendations on the way it ought to be used?
class VideoStream: UIViewController, ObservableObject, AVCaptureVideoDataOutputSampleBufferDelegate {
@Revealed var luminosityReading : Double = 0.0
...
// AVCaptureSession configuration entered, enter added, set up preview layer:
// Presently engaged on DispatchQueue(label: "VideoStreamSetupQueue")
layer = AVCaptureVideoPreviewLayer(session: session)
...
DispatchQueue.fundamental.async {
self.view.layer.addSublayer(self.layer)
}
// Set up output for luminosity calculation
let videoOutput = AVCaptureVideoDataOutput()
guard
session.canAddOutput(videoOutput)
else {
print("Error creating video output")
return
}
videoOutput.setSampleBufferDelegate(self, queue: DispatchQueue(label: "VideoStreamForCaptureOutputQueue"))
session.addOutput(videoOutput)
session.sessionPreset = .medium
session.commitConfiguration()
session.startRunning()
...
func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
// customized logic to calculate luminosity
DispatchQueue.fundamental.async {
print(luminosity) // worth modifications as anticipated
self.luminosityReading = luminosity // view not up to date with printed worth
}
}
Establishing a UIViewControllerRepresentable to show video preview layer in a SwiftUI view:
struct HostedVideoPreviewLayer: UIViewControllerRepresentable {
func makeUIViewController(context: Context) -> some UIViewController {
return VideoStream()
}
func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) {
// video preview layer works as anticipated
// textual content unrelated to this struct (see beneath) isn't updating
}
}
Creating the view:
struct ContentView: View {
@StateObject var videoStream = VideoStream()
var physique: some View {
VStack {
HostedVideoPreviewLayer()
Textual content(String(format: "%.2f Lux", videoStream.luminosityReading))
.font(.largeTitle)
.padding()
}
}
}
Minimal Reproducible Instance:
import Basis
import UIKit
import AVKit
import AVFoundation
import SwiftUI
struct ContentView: View {
@StateObject var videoStream = VideoStream()
var physique: some View {
VStack {
HostedVideoPreviewLayer()
Textual content(String(format: "%.2f Lux", videoStream.luminosityReading))
.font(.largeTitle)
.padding()
}
}
}
class VideoStream: UIViewController, ObservableObject, AVCaptureVideoDataOutputSampleBufferDelegate {
@Revealed var luminosityReading : Double = 0.0
personal let session = AVCaptureSession()
personal let queue = DispatchQueue(label: "VideoStreamSetupQueue")
personal var layer = AVCaptureVideoPreviewLayer()
var screenRect: CGRect!
override func viewDidLoad() {
authorizeCapture()
queue.async {
self.authorizeCapture()
}
}
func authorizeCapture() {
swap AVCaptureDevice.authorizationStatus(for: .video) {
case .licensed: // The person has beforehand granted entry to the digicam.
beginCapture()
case .notDetermined: // The person has not but been requested for digicam entry.
queue.droop()
AVCaptureDevice.requestAccess(for: .video) { granted in
if granted {
self.beginCapture()
self.queue.resume()
}
}
default:
return
}
}
func beginCapture() {
session.beginConfiguration()
let videoDevice = AVCaptureDevice.default(for: .video)
// Add machine as enter
guard
let videoDeviceInput = attempt? AVCaptureDeviceInput(machine: videoDevice!),
session.canAddInput(videoDeviceInput)
else {
print("Digicam choice failed")
return
}
session.addInput(videoDeviceInput)
// Set up preview layer
screenRect = UIScreen.fundamental.bounds
layer = AVCaptureVideoPreviewLayer(session: session)
layer.body = CGRect(x: 0, y: 0, width: screenRect.measurement.width, top: 300)
layer.videoGravity = AVLayerVideoGravity.resizeAspectFill
layer.connection?.videoOrientation = .portrait
DispatchQueue.fundamental.async {
self.view.layer.addSublayer(self.layer)
}
// Set up output for luminosity calculation
let videoOutput = AVCaptureVideoDataOutput()
guard
session.canAddOutput(videoOutput)
else {
print("Error creating video output")
return
}
videoOutput.setSampleBufferDelegate(self, queue: DispatchQueue(label: "VideoStreamForCaptureOutputQueue"))
session.addOutput(videoOutput)
session.sessionPreset = .medium
session.commitConfiguration()
session.startRunning()
}
// From: https://stackoverflow.com/questions/41921326/how-to-get-light-value-from-avfoundation/46842115#46842115
func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
// Retrieving EXIF information of camara body buffer
let rawMetadata = CMCopyDictionaryOfAttachments(allocator: nil, goal: sampleBuffer, attachmentMode: CMAttachmentMode(kCMAttachmentMode_ShouldPropagate))
let metadata = CFDictionaryCreateMutableCopy(nil, 0, rawMetadata) as NSMutableDictionary
let exifData = metadata.worth(forKey: "{Exif}") as? NSMutableDictionary
let FNumber : Double = exifData?["FNumber"] as! Double
let ExposureTime : Double = exifData?["ExposureTime"] as! Double
let ISOSpeedRatingsArray = exifData!["ISOSpeedRatings"] as? NSArray
let ISOSpeedRatings : Double = ISOSpeedRatingsArray![0] as! Double
let CalibrationConstant : Double = 50
//Calculating the luminosity
let luminosity : Double = (CalibrationConstant * FNumber * FNumber ) / ( ExposureTime * ISOSpeedRatings )
DispatchQueue.fundamental.async {
print(luminosity) // worth modifications as anticipated
self.luminosityReading = luminosity // view not up to date with current worth
}
}
override func willTransition(to newCollection: UITraitCollection, with coordinator: UIViewControllerTransitionCoordinator) {
screenRect = UIScreen.fundamental.bounds
layer.body = CGRect(x: 0, y: 0, width: screenRect.measurement.width, top: screenRect.measurement.top)
swap UIDevice.present.orientation {
// Residence button on high
case UIDeviceOrientation.portraitUpsideDown:
layer.connection?.videoOrientation = .portraitUpsideDown
// Residence button on proper
case UIDeviceOrientation.landscapeLeft:
layer.connection?.videoOrientation = .landscapeRight
// Residence button on left
case UIDeviceOrientation.landscapeRight:
layer.connection?.videoOrientation = .landscapeLeft
// Residence button at backside
case UIDeviceOrientation.portrait:
layer.connection?.videoOrientation = .portrait
default:
break
}
}
}
struct HostedVideoPreviewLayer: UIViewControllerRepresentable {
func makeUIViewController(context: Context) -> some UIViewController {
return VideoStream()
}
func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) {
// video preview layer works as anticipated
// textual content unrelated to this struct isn't updating
}
}
[1]: https://developer.apple.com/documentation/swiftui/uiviewcontrollerrepresentable/updateuiviewcontroller(_:context:)
[ad_2]
