import AVFoundation import Photos import SwiftUI import UIKit class MediaManager: NSObject, ObservableObject { // @Published var photos: [UIImage] = [] @Published var cameraFeed: UIImage? // @Published var galleryAccessLevel: PHAuthorizationStatus = .notDetermined @Published var cameraAccessLevel: AVAuthorizationStatus = AVCaptureDevice.authorizationStatus(for: .video) override init() { super.init() NotificationCenter.default.addObserver(self, selector: #selector(appDidBecomeActive), name: UIApplication.didBecomeActiveNotification, object: nil) // DispatchQueue.main.async { [weak self] in // // self?.fetchPhotos() // } } // private func fetchPhotos() { // galleryAccessLevel = PHPhotoLibrary.authorizationStatus() // // let fetchOptions = PHFetchOptions() // fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)] // let assets = PHAsset.fetchAssets(with: .image, options: fetchOptions) // // let manager = PHImageManager.default() // let option = PHImageRequestOptions() // option.isSynchronous = true // // assets.enumerateObjects { asset, _, _ in // manager.requestImage(for: asset, targetSize: CGSize(width: 200, height: 200), contentMode: .aspectFill, options: option) { image, _ in // if let image = image { // DispatchQueue.main.async { // self.photos.append(image) // } // } // } // } // } private func setupCameraFeed() { cameraAccessLevel = AVCaptureDevice.authorizationStatus(for: .video) let captureSession = AVCaptureSession() captureSession.sessionPreset = .medium guard let backCamera = AVCaptureDevice.default(.builtInWideAngleCamera, for: .video, position: .back) else { print("Unable to access the back camera!") return } do { let input = try AVCaptureDeviceInput(device: backCamera) if captureSession.canAddInput(input) { captureSession.addInput(input) } } catch { print("Error Unable to initialize back camera: \(error.localizedDescription)") } let videoOutput = AVCaptureVideoDataOutput() videoOutput.setSampleBufferDelegate(self, queue: DispatchQueue(label: "videoQueue")) if captureSession.canAddOutput(videoOutput) { captureSession.addOutput(videoOutput) } captureSession.startRunning() } } extension MediaManager: AVCaptureVideoDataOutputSampleBufferDelegate { func captureOutput(_: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from _: AVCaptureConnection) { print("Capturing output started") guard let pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else { return } let ciImage = CIImage(cvPixelBuffer: pixelBuffer) let context = CIContext() guard let cgImage = context.createCGImage(ciImage, from: ciImage.extent) else { return } DispatchQueue.main.async { self.cameraFeed = UIImage(cgImage: cgImage) print("Updated camera feed") } } } extension MediaManager { func openAppSettings() { if let appSettingsUrl = URL(string: UIApplication.openSettingsURLString), UIApplication.shared.canOpenURL(appSettingsUrl) { UIApplication.shared.open(appSettingsUrl, completionHandler: nil) } } @objc private func appDidBecomeActive() { // Update access levels // galleryAccessLevel = PHPhotoLibrary.authorizationStatus() cameraAccessLevel = AVCaptureDevice.authorizationStatus(for: .video) setupCameraFeed() } }