-
Notifications
You must be signed in to change notification settings - Fork 3
/
PhotoPickerView.swift
57 lines (47 loc) · 1.66 KB
/
PhotoPickerView.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//
// PhotoPickerView.swift
// PovioKit
//
// Created by Borut Tomazin on 10/02/2024.
// Copyright © 2024 Povio Inc. All rights reserved.
//
#if os(iOS)
import SwiftUI
import UIKit
/// This view should be used in conjuction with `PhotoPicker` view modifier.
public struct PhotoPickerView: UIViewControllerRepresentable {
let sourceType: UIImagePickerController.SourceType
let onComplete: (UIImage) -> Void
public func makeUIViewController(context: Context) -> UIImagePickerController {
let imagePicker = UIImagePickerController()
imagePicker.sourceType = sourceType
imagePicker.delegate = context.coordinator
return imagePicker
}
public func updateUIViewController(_ uiViewController: UIImagePickerController, context: Context) { /* --- */ }
public func makeCoordinator() -> Coordinator {
Coordinator(onComplete: onComplete)
}
}
// MARK: - Coordinator
public extension PhotoPickerView {
final class Coordinator: NSObject, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
let onComplete: (UIImage) -> Void
public init(onComplete: @escaping (UIImage) -> Void) {
self.onComplete = onComplete
}
public func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
guard let image = info[.originalImage] as? UIImage else {
picker.dismiss(animated: true)
return
}
picker.dismiss(animated: true) {
self.onComplete(image)
}
}
public func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
picker.dismiss(animated: true)
}
}
}
#endif