Tatsi icon indicating copy to clipboard operation
Tatsi copied to clipboard

Integration with SwiftUI

Open EstebanC02 opened this issue 4 years ago • 2 comments

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!

EstebanC02 avatar Apr 21 '21 00:04 EstebanC02

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 :)

renssies avatar Apr 24 '21 17:04 renssies

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()
  }

achuaswani avatar Apr 26 '21 02:04 achuaswani