another.im-ios/ConversationsClassic/View/Screens/Attachments/AttachmentMediaPickerView.swift
2024-07-02 11:56:27 +02:00

63 lines
1.9 KiB
Swift

import SwiftUI
import UIKit
struct AttachmentMediaPickerView: View {
@State private var isPickerPresented = false
@State private var mediaType: UIImagePickerController.SourceType = .photoLibrary
var body: some View {
VStack {
Button(action: {
mediaType = .photoLibrary
isPickerPresented = true
}) {
Text("Select from Photo Library")
}
Button(action: {
mediaType = .camera
isPickerPresented = true
}) {
Text("Take Photo or Video")
}
}
.sheet(isPresented: $isPickerPresented) {
ImagePicker(sourceType: mediaType)
}
}
}
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
}
}
}