-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eb81ae3
commit ec19ea0
Showing
2 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import Foundation | ||
|
||
public enum ImagePickerError: Error { | ||
case missingImage | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import SwiftUI | ||
import PhotosUI | ||
|
||
@available(iOS 14.0, *) | ||
public struct PHPicker: UIViewControllerRepresentable { | ||
@Binding var isPresented: Bool | ||
@Binding var images: [UIImage] | ||
|
||
var filter: PHPickerFilter? | ||
var selectionLimit: Int | ||
|
||
public init( | ||
filter: PHPickerFilter? = nil, | ||
selectionLimit: Int = 0, | ||
isPresented: Binding<Bool>, | ||
images: Binding<[UIImage]> | ||
) { | ||
self.filter = filter | ||
self.selectionLimit = selectionLimit | ||
self._isPresented = isPresented | ||
self._images = images | ||
} | ||
|
||
public func makeUIViewController(context: Context) -> PHPickerViewController { | ||
var configuration = PHPickerConfiguration() | ||
configuration.filter = filter | ||
configuration.selectionLimit = selectionLimit | ||
|
||
let picker = PHPickerViewController(configuration: configuration) | ||
picker.delegate = context.coordinator | ||
|
||
return picker | ||
} | ||
|
||
public func updateUIViewController(_ uiViewController: PHPickerViewController, context: Context) { | ||
} | ||
|
||
public func makeCoordinator() -> Coordinator { | ||
Coordinator(self) | ||
} | ||
|
||
public class Coordinator: PHPickerViewControllerDelegate { | ||
private let parent: PHPicker | ||
|
||
public init(_ parent: PHPicker) { | ||
self.parent = parent | ||
} | ||
|
||
public func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) { | ||
Task { | ||
for result in results { | ||
do { | ||
let image = try await loadImage(result: result) | ||
parent.images.append(image) | ||
} catch { | ||
print(error.localizedDescription) | ||
} | ||
} | ||
parent.isPresented = false | ||
} | ||
} | ||
|
||
private func loadImage(result: PHPickerResult) async throws -> UIImage { | ||
try await withCheckedThrowingContinuation { continuation in | ||
let provider = result.itemProvider | ||
provider.loadObject(ofClass: UIImage.self) { image, error in | ||
if let error { | ||
continuation.resume(throwing: error) | ||
return | ||
} | ||
guard let image = image as? UIImage else { | ||
continuation.resume(throwing: ImagePickerError.missingImage) | ||
return | ||
} | ||
continuation.resume(returning: image) | ||
} | ||
} | ||
} | ||
} | ||
} |