-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathImagePicker.swift
100 lines (83 loc) · 3.44 KB
/
ImagePicker.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import SwiftUI
import Firebase
/// ImagePickerRepresentable wraps a UIImagePickerController, so it is accessible through SwiftUI.
struct ImagePickerRepresentable {
enum Source {
case camera
case photoLibrary
}
/// Denotes whether the user is taking a photo or selecting one.
var source: Source
/// Persistent storage which retains the image.
@ObservedObject var store: ImageStore
/// Binds to whether the image picker is visible.
@Binding var visible: Bool
/// Completion handler that is invoked when the image picker dismisses.
var completion: () -> Void
/// Coordinator is an internal class that acts as a delegate for the image picker.
class Coordinator: NSObject {
private var representable: ImagePickerRepresentable
private var store: ImageStore
init(representable: ImagePickerRepresentable, store: ImageStore) {
self.representable = representable
self.store = store
}
}
}
extension ImagePickerRepresentable: UIViewControllerRepresentable {
typealias UIViewControllerType = UIImagePickerController
/// Invoked by the system to setup a coordinator that the UIImagePickerViewController can use.
/// - Returns: The coordinator.
func makeCoordinator() -> Coordinator {
Coordinator(representable: self, store: self.store)
}
func makeUIViewController(context: Context) -> UIImagePickerController {
let imagePicker = UIImagePickerController()
switch self.source {
case .camera:
imagePicker.sourceType = .camera
imagePicker.cameraCaptureMode = .photo
case .photoLibrary:
imagePicker.sourceType = .photoLibrary
}
imagePicker.delegate = context.coordinator
return imagePicker
}
/// Required to implement, but unnecessary. We do not need to invalidate the SwiftUI canvas.
func updateUIViewController(_ uiViewController: UIImagePickerController, context: Context) { }
}
extension ImagePickerRepresentable.Coordinator: UIImagePickerControllerDelegate {
func imagePickerController(
_ picker: UIImagePickerController,
didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]
) {
if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
// TODO: Consider displaying a progress bar or spinner here
store.saveImage(image) { result in
// TODO: Handle the error
self.representable.visible = false
picker.dismiss(animated: true, completion: self.representable.completion)
}
}
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
self.representable.visible = false
picker.dismiss(animated: true, completion: self.representable.completion)
}
}
/// The coordinator must implement the UINavigationControllerDelegate protocol in order to be the
/// UIImagePickerController's delegate.
extension ImagePickerRepresentable.Coordinator: UINavigationControllerDelegate { }