0

我正在通过 SPM 将Tatsi库集成到我在 SwiftUI 中的项目中,但是我遇到了一个问题,因为没有调用 Tatsi 视图顶部的 Done 并且我无法获取所选图像。

通过 UIViewControllerRepresentable 进行集成的代码如下:

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

我不知道为什么在选择图像时无法完成完成操作。

我以这种方式集成库是因为我需要对 iOS 13 的支持。

4

0 回答 0