Skip to content

Commit

Permalink
Update PHPicker.swift
Browse files Browse the repository at this point in the history
  • Loading branch information
tsuzukihashi committed May 18, 2024
1 parent ec19ea0 commit 1a29207
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion Sources/ImagePickerSwiftUI/PHPicker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import PhotosUI
public struct PHPicker: UIViewControllerRepresentable {
@Binding var isPresented: Bool
@Binding var images: [UIImage]
@Binding var videoURLs: [URL]

var filter: PHPickerFilter?
var selectionLimit: Int
Expand All @@ -13,12 +14,14 @@ public struct PHPicker: UIViewControllerRepresentable {
filter: PHPickerFilter? = nil,
selectionLimit: Int = 0,
isPresented: Binding<Bool>,
images: Binding<[UIImage]>
images: Binding<[UIImage]>,
videoURLs: Binding<[URL]>
) {
self.filter = filter
self.selectionLimit = selectionLimit
self._isPresented = isPresented
self._images = images
self._videoURLs = videoURLs
}

public func makeUIViewController(context: Context) -> PHPickerViewController {
Expand Down Expand Up @@ -51,7 +54,10 @@ public struct PHPicker: UIViewControllerRepresentable {
for result in results {
do {
let image = try await loadImage(result: result)
let videoURL = try await loadVideo(result: result)

parent.images.append(image)
parent.videoURLs.append(videoURL)
} catch {
print(error.localizedDescription)
}
Expand All @@ -60,6 +66,26 @@ public struct PHPicker: UIViewControllerRepresentable {
}
}

private func loadVideo(result: PHPickerResult) async throws -> URL {
try await withCheckedThrowingContinuation { continuation in
let provider = result.itemProvider

if provider.hasItemConformingToTypeIdentifier(UTType.movie.identifier) {
provider.loadFileRepresentation(forTypeIdentifier: UTType.movie.identifier) { fileURL, error in
if let error {
continuation.resume(throwing: error)
return
}
guard let fileURL else {
continuation.resume(throwing: ImagePickerError.missingImage)
return
}
continuation.resume(returning: fileURL)
}
}
}
}

private func loadImage(result: PHPickerResult) async throws -> UIImage {
try await withCheckedThrowingContinuation { continuation in
let provider = result.itemProvider
Expand Down

0 comments on commit 1a29207

Please sign in to comment.