mirror of
https://github.com/simplex-chat/simplex-chat.git
synced 2026-04-05 02:26:01 +00:00
104 lines
3.3 KiB
Swift
104 lines
3.3 KiB
Swift
//
|
|
// ImagePicker.swift
|
|
// SimpleX
|
|
//
|
|
// Created by Evgeny on 23/03/2022.
|
|
// Copyright © 2022 SimpleX Chat. All rights reserved.
|
|
//
|
|
|
|
import SwiftUI
|
|
import PhotosUI
|
|
|
|
struct LibraryImagePicker: UIViewControllerRepresentable {
|
|
typealias UIViewControllerType = PHPickerViewController
|
|
@Binding var image: UIImage?
|
|
var didFinishPicking: (_ didSelectItems: Bool) -> Void
|
|
|
|
class Coordinator: PHPickerViewControllerDelegate {
|
|
let parent: LibraryImagePicker
|
|
|
|
init(_ parent: LibraryImagePicker) {
|
|
self.parent = parent
|
|
}
|
|
|
|
func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
|
|
parent.didFinishPicking(!results.isEmpty)
|
|
guard !results.isEmpty else {
|
|
return
|
|
}
|
|
|
|
if let chosenImageProvider = results.first?.itemProvider {
|
|
if chosenImageProvider.canLoadObject(ofClass: UIImage.self) {
|
|
chosenImageProvider.loadObject(ofClass: UIImage.self) { [weak self] image, error in
|
|
DispatchQueue.main.async {
|
|
self?.loadImage(object: image, error: error)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func loadImage(object: Any?, error: Error? = nil) {
|
|
if let error = error {
|
|
logger.error("Couldn't load image with error: \(error.localizedDescription)")
|
|
}
|
|
parent.image = object as? UIImage
|
|
}
|
|
}
|
|
|
|
func makeCoordinator() -> Coordinator {
|
|
Coordinator(self)
|
|
}
|
|
|
|
func makeUIViewController(context: Context) -> PHPickerViewController {
|
|
var config = PHPickerConfiguration()
|
|
config.filter = .images
|
|
config.selectionLimit = 1
|
|
let controller = PHPickerViewController(configuration: config)
|
|
controller.delegate = context.coordinator
|
|
return controller
|
|
}
|
|
|
|
func updateUIViewController(_ uiViewController: PHPickerViewController, context: Context) {
|
|
|
|
}
|
|
}
|
|
|
|
|
|
struct CameraImagePicker: UIViewControllerRepresentable {
|
|
@Environment(\.presentationMode) var presentationMode
|
|
@Binding var image: UIImage?
|
|
|
|
class Coordinator: NSObject, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
|
|
let parent: CameraImagePicker
|
|
|
|
init(_ parent: CameraImagePicker) {
|
|
self.parent = parent
|
|
}
|
|
|
|
func imagePickerController(_ picker: UIImagePickerController,
|
|
didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
|
|
if let uiImage = info[.originalImage] as? UIImage {
|
|
parent.image = uiImage
|
|
}
|
|
parent.presentationMode.wrappedValue.dismiss()
|
|
}
|
|
}
|
|
|
|
func makeCoordinator() -> Coordinator {
|
|
Coordinator(self)
|
|
}
|
|
|
|
func makeUIViewController(context: UIViewControllerRepresentableContext<CameraImagePicker>) -> UIImagePickerController {
|
|
let picker = UIImagePickerController()
|
|
picker.sourceType = .camera
|
|
picker.allowsEditing = false
|
|
picker.delegate = context.coordinator
|
|
return picker
|
|
}
|
|
|
|
func updateUIViewController(_ uiViewController: UIImagePickerController, context: UIViewControllerRepresentableContext<CameraImagePicker>) {
|
|
|
|
}
|
|
}
|