mirror of
https://github.com/permissionlesstech/bitchat.git
synced 2026-07-25 18:25:19 +00:00
69 lines
2.4 KiB
Swift
69 lines
2.4 KiB
Swift
// ImagePickerView.swift
|
|
// Simple UIKit-based image picker bridge
|
|
|
|
import SwiftUI
|
|
#if os(iOS)
|
|
import UIKit
|
|
import PhotosUI
|
|
|
|
struct ImagePickerView: UIViewControllerRepresentable {
|
|
typealias UIViewControllerType = UIViewController
|
|
var onPicked: (UIImage?) -> Void
|
|
|
|
func makeUIViewController(context: Context) -> UIViewController {
|
|
if #available(iOS 14.0, *) {
|
|
var config = PHPickerConfiguration(photoLibrary: .shared())
|
|
config.filter = .images
|
|
config.selectionLimit = 1
|
|
let picker = PHPickerViewController(configuration: config)
|
|
picker.delegate = context.coordinator
|
|
return picker
|
|
} else {
|
|
let picker = UIImagePickerController()
|
|
picker.delegate = context.coordinator
|
|
picker.sourceType = .photoLibrary
|
|
picker.allowsEditing = false
|
|
return picker
|
|
}
|
|
}
|
|
|
|
func updateUIViewController(_ uiViewController: UIViewController, context: Context) {}
|
|
|
|
func makeCoordinator() -> Coordinator {
|
|
Coordinator(onPicked: onPicked)
|
|
}
|
|
|
|
final class Coordinator: NSObject, PHPickerViewControllerDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
|
|
let onPicked: (UIImage?) -> Void
|
|
init(onPicked: @escaping (UIImage?) -> Void) { self.onPicked = onPicked }
|
|
|
|
// PHPicker
|
|
@available(iOS 14.0, *)
|
|
func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
|
|
picker.dismiss(animated: true)
|
|
guard let item = results.first else { onPicked(nil); return }
|
|
if item.itemProvider.canLoadObject(ofClass: UIImage.self) {
|
|
item.itemProvider.loadObject(ofClass: UIImage.self) { object, _ in
|
|
DispatchQueue.main.async {
|
|
self.onPicked(object as? UIImage)
|
|
}
|
|
}
|
|
} else {
|
|
onPicked(nil)
|
|
}
|
|
}
|
|
|
|
// UIImagePicker
|
|
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
|
|
picker.dismiss(animated: true)
|
|
onPicked(nil)
|
|
}
|
|
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
|
|
let image = info[.originalImage] as? UIImage
|
|
picker.dismiss(animated: true)
|
|
onPicked(image)
|
|
}
|
|
}
|
|
}
|
|
#endif
|