-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPhotoPicker.swift
94 lines (78 loc) · 2.74 KB
/
PhotoPicker.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
//
// File.swift
//
//
// Created by Shunzhe Ma on 8/15/20.
//
#if os(iOS) && !targetEnvironment(macCatalyst)
import Foundation
import SwiftUI
import PhotosUI
/**
# Example #
ユーザーが1つの画像を選択できるようにするには
```
PhotoPickerView(onImagePicked: { (image) in
print(image)
})
```
ユーザーが複数の画像を選択できるようにするには
```
PhotoPickerView(onImagePicked: { (image) in
print(image)
}, selectionLimit: 2)
```
動画のみを表示するには
```
PhotoPickerView(onImagePicked: { (image) in
print(image)
}, filter: .videos)
```
*/
@available(iOS 14.0, *)
public struct PhotoPickerView: UIViewControllerRepresentable {
private let onImagePicked: (UIImage) -> Void
private var filter: PHPickerFilter? = nil
private var selectionLimit: Int = 1
@Environment(\.presentationMode) private var presentationMode
public init(onImagePicked: @escaping (UIImage) -> Void, selectionLimit: Int = 1, filter: PHPickerFilter? = nil) {
self.onImagePicked = onImagePicked
self.filter = filter
self.selectionLimit = selectionLimit
}
public func makeUIViewController(context: Context) -> PHPickerViewController {
let photoLibrary = PHPhotoLibrary.shared()
var config = PHPickerConfiguration(photoLibrary: photoLibrary)
config.filter = self.filter
config.selectionLimit = self.selectionLimit
let picker = PHPickerViewController(configuration: config)
picker.delegate = context.coordinator
return picker
}
public func updateUIViewController(_ uiViewController: PHPickerViewController, context: Context) {}
public func makeCoordinator() -> Coordinator {
Coordinator(onImagePicked: self.onImagePicked)
}
final public class Coordinator: NSObject, PHPickerViewControllerDelegate {
private let onImagePicked: (UIImage) -> Void
public init(onImagePicked: @escaping (UIImage) -> Void) {
self.onImagePicked = onImagePicked
}
public func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
picker.dismiss(animated: true, completion: nil)
for result in results {
// Check if we can load the objects
result.itemProvider.loadObject(ofClass: UIImage.self) { (image, error) in
guard error == nil,
let imageObject = image as? UIImage
else {
print(error?.localizedDescription ?? "Unknown error")
return
}
self.onImagePicked(imageObject)
}
}
}
}
}
#endif