GithubHelp home page GithubHelp logo

swiftui-multiple-imagepicker's Introduction

swiftui-multiple-imagepicker

//
//  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 People

Contributors

paigeshin avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.