wip
This commit is contained in:
parent
778130ac65
commit
024c9d85c8
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,107 +1,57 @@
|
|||
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?
|
||||
// @StateObject private var mediaManager = MediaManager()
|
||||
|
||||
var hasCam: Bool = false
|
||||
|
||||
let elements = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J"]
|
||||
|
||||
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)
|
||||
LazyVGrid(columns: Array(repeating: .init(.flexible()), count: 3)) {
|
||||
ForEach(0 ..< 10) { index in
|
||||
if index == 0 {
|
||||
VStack {
|
||||
Color.red
|
||||
.frame(height: 100)
|
||||
Color.red
|
||||
.frame(height: 100)
|
||||
}
|
||||
}
|
||||
|
||||
ForEach(mediaManager.photos, id: \.self) { photo in
|
||||
Button(action: {
|
||||
selectedPhoto = photo
|
||||
}) {
|
||||
Image(uiImage: photo)
|
||||
.resizable()
|
||||
.aspectRatio(contentMode: .fill)
|
||||
} else {
|
||||
Color.blue
|
||||
.frame(height: 100)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
.sheet(isPresented: $isPickerPresented) {
|
||||
ImagePicker(sourceType: .camera)
|
||||
.padding(.horizontal)
|
||||
// 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)
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct ImagePicker: UIViewControllerRepresentable {
|
||||
var sourceType: UIImagePickerController.SourceType
|
||||
|
||||
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
|
||||
}
|
||||
struct AttachmentMediaPickerView_Previews: PreviewProvider {
|
||||
static var previews: some View {
|
||||
AttachmentMediaPickerView()
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue