another.im-ios/ConversationsClassic/View/Screens/Attachments/AttachmentMediaManager.swift

111 lines
3.8 KiB
Swift
Raw Normal View History

2024-07-03 08:53:39 +00:00
import AVFoundation
import Photos
import SwiftUI
import UIKit
class MediaManager: NSObject, ObservableObject {
2024-07-03 10:47:59 +00:00
// @Published var photos: [UIImage] = []
2024-07-03 08:53:39 +00:00
@Published var cameraFeed: UIImage?
2024-07-03 10:47:59 +00:00
// @Published var galleryAccessLevel: PHAuthorizationStatus = .notDetermined
@Published var cameraAccessLevel: AVAuthorizationStatus = AVCaptureDevice.authorizationStatus(for: .video)
2024-07-03 08:53:39 +00:00
override init() {
super.init()
2024-07-03 10:47:59 +00:00
NotificationCenter.default.addObserver(self, selector: #selector(appDidBecomeActive), name: UIApplication.didBecomeActiveNotification, object: nil)
2024-07-03 08:53:39 +00:00
2024-07-03 10:47:59 +00:00
// DispatchQueue.main.async { [weak self] in
// // self?.fetchPhotos()
// }
2024-07-03 08:53:39 +00:00
}
2024-07-03 10:47:59 +00:00
// 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)
// }
// }
// }
// }
// }
2024-07-03 08:53:39 +00:00
private func setupCameraFeed() {
2024-07-03 10:47:59 +00:00
cameraAccessLevel = AVCaptureDevice.authorizationStatus(for: .video)
2024-07-03 08:53:39 +00:00
let captureSession = AVCaptureSession()
captureSession.sessionPreset = .medium
2024-07-03 10:47:59 +00:00
guard let backCamera = AVCaptureDevice.default(.builtInWideAngleCamera, for: .video, position: .back) else {
print("Unable to access the back camera!")
2024-07-03 08:53:39 +00:00
return
}
do {
2024-07-03 10:47:59 +00:00
let input = try AVCaptureDeviceInput(device: backCamera)
if captureSession.canAddInput(input) {
captureSession.addInput(input)
}
2024-07-03 08:53:39 +00:00
} catch {
2024-07-03 10:47:59 +00:00
print("Error Unable to initialize back camera: \(error.localizedDescription)")
2024-07-03 08:53:39 +00:00
}
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) {
2024-07-03 10:47:59 +00:00
print("Capturing output started")
2024-07-03 08:53:39 +00:00
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)
2024-07-03 10:47:59 +00:00
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)
2024-07-03 08:53:39 +00:00
}
}
2024-07-03 10:47:59 +00:00
@objc private func appDidBecomeActive() {
// Update access levels
// galleryAccessLevel = PHPhotoLibrary.authorizationStatus()
cameraAccessLevel = AVCaptureDevice.authorizationStatus(for: .video)
setupCameraFeed()
}
2024-07-03 08:53:39 +00:00
}