@@ -10,28 +10,45 @@ import Foundation
10
10
import UIKit
11
11
import PhotosUI
12
12
13
+ /// A protocol that defines the delegate method for handling the selected image.
13
14
public protocol ImagePickerDelegate : AnyObject {
15
+ /// Called when an image is selected from the image picker.
16
+ ///
17
+ /// - Parameter image: The selected image, or nil if no image was selected.
14
18
func didSelect( image: UIImage ? )
15
19
}
16
20
17
-
21
+ /// A class that provides a system-provided interface for selecting an image from the camera roll or photo library.
18
22
class PhotoPicker : NSObject {
19
23
24
+ // The image picker controller used to present the system interface.
20
25
private let pickerController : UIImagePickerController
26
+
27
+ // A weak reference to the view controller that presents the image picker.
21
28
private weak var presentationController : UIViewController ?
29
+
30
+ // A weak reference to the delegate that will handle the selected image.
22
31
private weak var delegate : ImagePickerDelegate ?
23
32
24
- public init ( presentationController: UIViewController , delegate: ImagePickerDelegate ) {
33
+ /// Initializes a new instance of the `PhotoPicker` class.
34
+ ///
35
+ /// - Parameters:
36
+ /// - presentationController: The view controller that will present the image picker.
37
+ /// - delegate: The delegate that will handle the selected image.
38
+ public init ( presentationController: UIViewController ,
39
+ delegate: ImagePickerDelegate ) {
25
40
26
41
self . pickerController = UIImagePickerController ( )
27
42
super. init ( )
28
43
self . presentationController = presentationController
29
44
self . delegate = delegate
30
45
self . pickerController. delegate = self
31
46
self . pickerController. allowsEditing = false
32
-
33
47
}
34
48
49
+ /// Presents the image picker interface from the specified source view.
50
+ ///
51
+ /// - Parameter sourceView: The view from which the image picker should be presented.
35
52
public func present( from sourceView: UIView ) {
36
53
37
54
let alertController = UIAlertController ( title: nil , message: nil , preferredStyle: . actionSheet)
@@ -56,19 +73,29 @@ class PhotoPicker: NSObject {
56
73
self . presentationController? . present ( alertController, animated: true )
57
74
}
58
75
76
+ // A private method that handles the selected image and notifies the delegate.
59
77
private func pickerController( _ controller: UIImagePickerController , didSelect image: UIImage ? ) {
60
78
controller. dismiss ( animated: true , completion: nil )
61
79
self . delegate? . didSelect ( image: image)
62
80
}
63
81
}
64
82
83
+ // MARK: - UIImagePickerControllerDelegate
84
+
65
85
extension PhotoPicker : UIImagePickerControllerDelegate {
66
86
87
+ /// Called when the image picker is canceled.
88
+ ///
89
+ /// - Parameter picker: The image picker controller that was canceled.
67
90
public func imagePickerControllerDidCancel( _ picker: UIImagePickerController ) {
68
-
69
91
picker. dismiss ( animated: true )
70
92
}
71
93
94
+ /// Called when an image has been selected from the image picker.
95
+ ///
96
+ /// - Parameters:
97
+ /// - picker: The image picker controller that selected the image.
98
+ /// - info: A dictionary containing information about the selected media.
72
99
public func imagePickerController( _ picker: UIImagePickerController ,
73
100
didFinishPickingMediaWithInfo info: [ UIImagePickerController . InfoKey : Any ] ) {
74
101
guard let image = info [ . originalImage] as? UIImage else {
@@ -78,17 +105,33 @@ extension PhotoPicker: UIImagePickerControllerDelegate {
78
105
}
79
106
}
80
107
108
+ // MARK: - UINavigationControllerDelegate
109
+
81
110
extension PhotoPicker : UINavigationControllerDelegate { }
82
111
112
+ // MARK: - PHPhotoPicker (Available on iOS 14 and later)
113
+
83
114
@available ( iOS 14 , * )
84
- extension PHPhotoPicker : UINavigationControllerDelegate { }
115
+ extension PHPhotoPicker : UINavigationControllerDelegate { }
85
116
117
+ /// A class that provides an interface for selecting an image using the Photos UI on iOS 14 and later.
86
118
@available ( iOS 14 , * )
87
119
class PHPhotoPicker : NSObject {
120
+
121
+ // The Photos UI picker controller used to present the interface.
88
122
private let pickerController : PHPickerViewController
123
+
124
+ // A weak reference to the view controller that presents the Photos UI picker.
89
125
private weak var presentationController : UIViewController ?
126
+
127
+ // A weak reference to the delegate that will handle the selected image.
90
128
private weak var delegate : ImagePickerDelegate ?
91
129
130
+ /// Initializes a new instance of the `PHPhotoPicker` class.
131
+ ///
132
+ /// - Parameters:
133
+ /// - presentationController: The view controller that will present the Photos UI picker.
134
+ /// - delegate: The delegate that will handle the selected image.
92
135
public init ( presentationController: UIViewController , delegate: ImagePickerDelegate ) {
93
136
var phPickerConfiguration = PHPickerConfiguration ( photoLibrary: . shared( ) )
94
137
phPickerConfiguration. selectionLimit = 1
@@ -100,26 +143,32 @@ class PHPhotoPicker: NSObject {
100
143
self . delegate = delegate
101
144
}
102
145
146
+ /// Requests authorization to access the photo library and presents the Photos UI picker if authorized.
103
147
public func present( ) {
104
- PHPhotoLibrary . requestAuthorization ( {
105
- ( newStatus) in
148
+ PHPhotoLibrary . requestAuthorization ( { ( newStatus) in
106
149
DispatchQueue . main. async {
107
150
if newStatus == PHAuthorizationStatus . authorized {
108
-
109
- self . presentationController ? . present ( self . pickerController , animated: true )
151
+ self . presentationController ? . present ( self . pickerController ,
152
+ animated: true )
110
153
}
111
154
}
112
155
} )
113
156
}
114
-
115
157
}
116
158
159
+ // MARK: - PHPickerViewControllerDelegate
160
+
117
161
@available ( iOS 14 , * )
118
162
extension PHPhotoPicker : PHPickerViewControllerDelegate {
119
163
120
- func picker( _ picker: PHPickerViewController , didFinishPicking results: [ PHPickerResult ] ) {
164
+ /// Called when an image has been selected from the Photos UI picker.
165
+ ///
166
+ /// - Parameters:
167
+ /// - picker: The Photos UI picker controller that selected the image.
168
+ /// - results: An array of `PHPickerResult` objects representing the selected images.
169
+ func picker( _ picker: PHPickerViewController ,
170
+ didFinishPicking results: [ PHPickerResult ] ) {
121
171
picker. dismiss ( animated: true , completion: nil )
122
-
123
172
for result in results {
124
173
result. itemProvider. loadObject ( ofClass: UIImage . self, completionHandler: { ( object, error) in
125
174
if let image = object as? UIImage {
@@ -130,8 +179,6 @@ extension PHPhotoPicker: PHPickerViewControllerDelegate {
130
179
} )
131
180
}
132
181
}
133
-
134
182
}
135
183
136
184
#endif
137
-
0 commit comments