import AVFoundation import Photos 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 { @StateObject private var mediaManager = MediaManager() @State private var isPickerPresented = false @State private var selectedPhoto: UIImage? var body: some View { ScrollView { LazyVGrid(columns: [GridItem(), GridItem(), GridItem()]) { 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) } } } } .sheet(isPresented: $isPickerPresented) { ImagePicker(sourceType: .camera) } } } struct ImagePicker: UIViewControllerRepresentable { var sourceType: UIImagePickerController.SourceType func makeUIViewController(context: UIViewControllerRepresentableContext) -> UIImagePickerController { let picker = UIImagePickerController() picker.sourceType = sourceType picker.allowsEditing = true picker.delegate = context.coordinator return picker } func updateUIViewController(_: UIImagePickerController, context _: UIViewControllerRepresentableContext) {} 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 } } }