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 bd257f7 commit 9edcfed
Showing 1 changed file with 32 additions and 20 deletions.
52 changes: 32 additions & 20 deletions Sources/ImagePickerSwiftUI/PHPicker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,53 +53,65 @@ public struct PHPicker: UIViewControllerRepresentable {
Task {
for result in results {
do {
let image = try await loadImage(result: result)
if let image = try await loadImage(result: result) {
parent.images.append(image)
}
if let videoURL = try await loadVideo(result: result) {
parent.videoURLs.append(videoURL)
}

parent.images.append(image)

if results.last == result {
parent.isPresented = false
}
} catch {
print(error.localizedDescription)
}
}
parent.isPresented = false
}
}

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)
}
guard provider.hasItemConformingToTypeIdentifier(UTType.movie.identifier) else {
continuation.resume(returning: nil)
return
}

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

let fileName = "\(Int(Date().timeIntervalSince1970)).\(fileURL.pathExtension)"
let tmpUrl = URL(fileURLWithPath: NSTemporaryDirectory() + fileName)
try? FileManager.default.copyItem(at: fileURL, to: tmpUrl)
continuation.resume(returning: tmpUrl)
}
}
}

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

guard provider.hasItemConformingToTypeIdentifier(UTType.image.identifier) else {
continuation.resume(returning: nil)
return
}

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)
continuation.resume(returning: nil)
return
}
continuation.resume(returning: image)
Expand Down

0 comments on commit 9edcfed

Please sign in to comment.