This commit is contained in:
fmodf 2024-07-03 10:53:39 +02:00
parent 778130ac65
commit 024c9d85c8
2 changed files with 125 additions and 92 deletions

View file

@ -0,0 +1,83 @@
import AVFoundation
import Photos
import SwiftUI
import UIKit
class MediaManager: NSObject, ObservableObject {
@Published var photos: [UIImage] = []
@Published var cameraFeed: UIImage?
override init() {
super.init()
DispatchQueue.global(qos: .userInitiated).async { [weak self] in
self?.fetchPhotos()
self?.setupCameraFeed()
}
}
private func fetchPhotos() {
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() {
let captureSession = AVCaptureSession()
captureSession.sessionPreset = .medium
guard let captureDevice = AVCaptureDevice.default(for: .video) else {
return
}
let deviceInput: AVCaptureDeviceInput
do {
deviceInput = try AVCaptureDeviceInput(device: captureDevice)
} catch {
return
}
if captureSession.canAddInput(deviceInput) {
captureSession.addInput(deviceInput)
}
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) {
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)
}
}
}

View file

@ -1,107 +1,57 @@
import AVFoundation
import Photos
import SwiftUI import SwiftUI
import UIKit
class MediaManager: ObservableObject {
@Published var photos: [UIImage] = []
@Published var cameraFeed: UIImage?
init() {
fetchPhotos()
setupCameraFeed()
}
private func fetchPhotos() {
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: 100, height: 100), contentMode: .aspectFit, options: option) { image, _ in
if let image = image {
DispatchQueue.main.async {
self.photos.append(image)
}
}
}
}
}
private func setupCameraFeed() {
// Setup the camera feed and store the feed in the `cameraFeed` property
}
}
struct AttachmentMediaPickerView: View { struct AttachmentMediaPickerView: View {
@StateObject private var mediaManager = MediaManager() // @StateObject private var mediaManager = MediaManager()
@State private var isPickerPresented = false
@State private var selectedPhoto: UIImage? var hasCam: Bool = false
let elements = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J"]
var body: some View { var body: some View {
ScrollView { ScrollView {
LazyVGrid(columns: [GridItem(), GridItem(), GridItem()]) { LazyVGrid(columns: Array(repeating: .init(.flexible()), count: 3)) {
if let cameraFeed = mediaManager.cameraFeed { ForEach(0 ..< 10) { index in
Button(action: { if index == 0 {
isPickerPresented = true VStack {
}) { Color.red
Image(uiImage: cameraFeed) .frame(height: 100)
.resizable() Color.red
.aspectRatio(contentMode: .fill) .frame(height: 100)
} }
} } else {
Color.blue
ForEach(mediaManager.photos, id: \.self) { photo in .frame(height: 100)
Button(action: {
selectedPhoto = photo
}) {
Image(uiImage: photo)
.resizable()
.aspectRatio(contentMode: .fill)
} }
} }
} }
} .padding(.horizontal)
.sheet(isPresented: $isPickerPresented) { // LazyVGrid(columns: [GridItem(), GridItem(), GridItem()]) {
ImagePicker(sourceType: .camera) // if let cameraFeed = mediaManager.cameraFeed {
// Button(action: {
// isPickerPresented = true
// }) {
// Image(uiImage: cameraFeed)
// .resizable()
// .aspectRatio(contentMode: .fill)
// }
// }
//
// ForEach(mediaManager.photos, id: \.self) { photo in
// Button(action: {
// selectedPhoto = photo
// }) {
// Image(uiImage: photo)
// .resizable()
// .aspectRatio(contentMode: .fill)
// }
// }
// }
} }
} }
} }
struct ImagePicker: UIViewControllerRepresentable { struct AttachmentMediaPickerView_Previews: PreviewProvider {
var sourceType: UIImagePickerController.SourceType static var previews: some View {
AttachmentMediaPickerView()
func makeUIViewController(context: UIViewControllerRepresentableContext<ImagePicker>) -> UIImagePickerController {
let picker = UIImagePickerController()
picker.sourceType = sourceType
picker.allowsEditing = true
picker.delegate = context.coordinator
return picker
}
func updateUIViewController(_: UIImagePickerController, context _: UIViewControllerRepresentableContext<ImagePicker>) {}
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
class Coordinator: NSObject, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
var parent: ImagePicker
init(_ parent: ImagePicker) {
self.parent = parent
}
func imagePickerController(_: UIImagePickerController, didFinishPickingMediaWithInfo _: [UIImagePickerController.InfoKey: Any]) {
// Handle the selected media
}
func imagePickerControllerDidCancel(_: UIImagePickerController) {
// Handle cancellation
}
} }
} }