Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integration with SwiftUI #54

Open
EstebanC02 opened this issue Apr 21, 2021 · 2 comments
Open

Integration with SwiftUI #54

EstebanC02 opened this issue Apr 21, 2021 · 2 comments

Comments

@EstebanC02
Copy link

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
Copy link
Contributor

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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants