//
// MultipleImagePicker.swift
// ProteinTracker
//
// Created by paige shin on 2023/04/29.
//
import Foundation
import SwiftUI
import PhotosUI
struct MultipleImagePicker: UIViewControllerRepresentable {
let onSelectImages: ([UIImage]) -> Void
let onCancel: () -> Void
var selectionLimit = 36
func makeUIViewController(context: Context) -> PHPickerViewController {
var configuration = PHPickerConfiguration()
configuration.selectionLimit = self.selectionLimit // Selection limit. Set to 0 for unlimited.
configuration.filter = .images // he types of media that can be get.
let picker = PHPickerViewController(configuration: configuration)
picker.delegate = context.coordinator
return picker
}
func updateUIViewController(_ uiViewController: PHPickerViewController, context: Context) {
}
func makeCoordinator() -> MultipleImagePickerCoordinator {
return MultipleImagePickerCoordinator(onSelectImages: self.onSelectImages, onCancel: self.onCancel)
}
class MultipleImagePickerCoordinator: NSObject, PHPickerViewControllerDelegate {
private let onSelectImages: ([UIImage]) -> Void
private let onCancel: () -> Void
init(onSelectImages: @escaping([UIImage]) -> Void, onCancel: @escaping() -> Void) {
self.onSelectImages = onSelectImages
self.onCancel = onCancel
}
func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
Task {
var images = [UIImage]()
for result in results {
do {
if let image = try await self.loadImage(provider: result.itemProvider) {
images.append(image)
}
} catch {
Log.error(error)
continue
}
}
self.onSelectImages(images)
}
}
private func loadImage(provider: NSItemProvider) async throws -> UIImage? {
return try await withCheckedThrowingContinuation { continuation in
guard provider.canLoadObject(ofClass: UIImage.self) else {
continuation.resume(returning: nil)
return
}
provider.loadObject(ofClass: UIImage.self) { image, error in
if let error {
continuation.resume(throwing: error)
return
}
if let image = image as? UIImage {
continuation.resume(returning: image)
return
}
continuation.resume(returning: nil)
}
}
}
}
}
swiftui-multiple-imagepicker's Introduction
swiftui-multiple-imagepicker's People
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.