awkward / Tatsi

A drop-in replacement for UIImagePickerController with the ability to select multiple images and/or videos
MIT License
110 stars 30 forks source link

Integration with SwiftUI #54

Open EstebanC02 opened 3 years ago

EstebanC02 commented 3 years ago

Hi, I am doing the library integration via SPM to my project in SwiftUI, but I have a problem because the Done at the top of the Tatsi view is not called and I can't get the selected images.

The code for the integration via UIViewControllerRepresentable is as follows:

import SwiftUI
import UIKit
// 1. Add Import Tatsi and Import Photos to your Swift
import Tatsi
import Photos

struct TatsiViewController: UIViewControllerRepresentable {

    @Binding var show: Bool

    func makeCoordinator() -> Coordinator {
        Coordinator(self)
    }

    func makeUIViewController(context: UIViewControllerRepresentableContext<TatsiViewController>) -> TatsiPickerViewController {

        // 2. (Optional) Create an instance of TatsiConfig and configure the settings.
        var config = TatsiConfig.default
        config.showCameraOption = false
        config.supportedMediaTypes = [.image]
        config.firstView = .userLibrary

        // 3. Create an instance of TatsiPickerViewController. TatsiPickerViewController(config:) allows you to use the config from the previous step
        let pickerViewController = TatsiPickerViewController(config: config)
        // 5. Set the pickerDelegate on TatsiPickerViewController
        pickerViewController.delegate = context.coordinator
        // 6. Present the TatsiPickerViewController
        return pickerViewController
    }

    func updateUIViewController(_ uiViewController: TatsiPickerViewController, context: UIViewControllerRepresentableContext<TatsiViewController>) {
        uiViewController.delegate = context.coordinator
    }

    // 4. Implement TatsiPickerViewControllerDelegate
    final class Coordinator: NSObject, TatsiPickerViewControllerDelegate, UINavigationControllerDelegate {

        var parent: TatsiViewController

        init(_ parent: TatsiViewController) {
            self.parent = parent
        }

        func pickerViewController(_ pickerViewController: TatsiPickerViewController, didPickAssets assets: [PHAsset]) {
            print("Picked assets: \(assets)")
            parent.show = false
        }

        func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
            picker.dismiss(animated: true, completion: nil)
            parent.show = false
        }

        func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
            picker.dismiss(animated: true, completion: nil)
            parent.show = false
        }
    }
}

What do I need to integrate in order to obtain the selected images? Thank you!

renssies commented 3 years ago

What you need to do is implement the following methods in your coordinator:

func pickerViewController(_ pickerViewController: TatsiPickerViewController, didPickAssets assets: [PHAsset])

Is what will be called when the done button is pressed, with the assets the user has selected. Here you are also responsible for dismissing the pickerViewController yourself.

Next you also need to implement:

func pickerViewControllerDidCancel(_ pickerViewController: TatsiPickerViewController)

Which is called when the user taps the cancel button, in this case you also have to dismiss the pickerViewController yourself.

Any methods from UINavigationControllerDelegate or UIImagePickerControllerDelegate do not have to be implemented.

I hope this helps you :)

achuaswani commented 3 years ago

I also tried the same, where I am using the below code. But both cancel and done are not working as expected.

func pickerViewController(_ pickerViewController: TatsiPickerViewController, didPickAssets assets: [PHAsset]) {
      parent.isShown.toggle()
  }

  func pickerViewControllerDidCancel(_ pickerViewController: TatsiPickerViewController) {
      pickerViewController.dismiss(animated: true, completion: nil)
      parent.isShown.toggle()
  }