该函数在UIViewControllerRepresentable中不起作用

时间:2020-01-17 07:21:40

标签: swift uiviewcontroller swiftui

我正在尝试通过UIViewControllerRepresentable在SwiftUI中实现unsplash-photopicker-ios组件。视图成功启动,但是unsplashPhotoPicker函数不起作用,它始终返回nil,有人可以告诉我问题出在哪里。

import SwiftUI
import UnsplashPhotoPicker

struct UnsplashImagePicker: UIViewControllerRepresentable {

    var urlUnsplashImage = [UnsplashPhoto]()

    let configuration = UnsplashPhotoPickerConfiguration(
      accessKey: "f99d21d6eb682196455dd29b621688aff2d525c7c3a7f95bfcb05d497f38f5dc",
      secretKey: "ccff858162e795c062ce13e9d16a2cf607076d0eb185141e35b14f589b1cd713",
      allowsMultipleSelection: false)

    func makeUIViewController(context: UIViewControllerRepresentableContext<UnsplashImagePicker>) -> UnsplashPhotoPicker {
        let unsplashImagePicker = UnsplashPhotoPicker(configuration: configuration)
        unsplashImagePicker.delegate = context.coordinator
        return unsplashImagePicker
    }

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

    class Coordinator: NSObject, UnsplashPhotoPickerDelegate, UINavigationControllerDelegate {
        var parent: UnsplashImagePicker
        init(_ parent: UnsplashImagePicker) {
            self.parent = parent
        }

        func unsplashPhotoPicker(_ photoPicker: UnsplashPhotoPicker, didSelectPhotos photos: [UnsplashPhoto]) {
            print(photos)
        }

        func unsplashPhotoPickerDidCancel(_ photoPicker: UnsplashPhotoPicker) {
            print("Unsplash photo picker did cancel")
        }
    }

    func updateUIViewController(_ uiViewController: UnsplashPhotoPicker, context: UIViewControllerRepresentableContext<UnsplashImagePicker>) {

    }
}

1 个答案:

答案 0 :(得分:-1)

您需要从不同的 UIViewcontroller 呈现 Unsplashpicker。Unsplashpicker 不能直接用作 UIViewControllerRepresentable。

import SwiftUI 
import UIKit
import UnsplashPhotoPicker

struct UnsplashPresenter: UIViewControllerRepresentable {
    func updateUIViewController(_ uiViewController: UIViewController, context: Context) {
//
    }
    
    func makeUIViewController(context: Context) -> UIViewController {
        return UnsplashPickerVC()
    }
    
    typealias UIViewControllerType = UIViewController
    
}


class UnsplashPickerVC: UIViewController, UnsplashPhotoPickerDelegate {
    func unsplashPhotoPicker(_ photoPicker: UnsplashPhotoPicker, didSelectPhotos photos: [UnsplashPhoto]) {
        print("=============== New Photo Selected =============== \n")
        print(photos)
    }
    
    func unsplashPhotoPickerDidCancel(_ photoPicker: UnsplashPhotoPicker) {
        print("did cancel")
    }
    
    var outputImage = UIImage(named: "Instructions.png")
    
    @IBOutlet weak var imageView: UIImageView!
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        let button = UIButton()
        button.frame = CGRect(x: (self.view.frame.size.width / 2) - 25, y: (self.view.frame.size.height / 2) - 50, width: 100, height: 50)
        button.backgroundColor = UIColor.blue
        button.setTitle("Pick Images", for: .normal)
        button.addTarget(self, action: #selector(pickImages), for: .touchUpInside)
        self.view.addSubview(button)
        
        
    }
    
    @objc func pickImages(sender: UIBarButtonItem) {
        let configuration = UnsplashPhotoPickerConfiguration(
          accessKey: "<Your Access Key >",
          secretKey: "<Your Secret Key >"
        )

        let photoPicker = UnsplashPhotoPicker(configuration: configuration)
        photoPicker.photoPickerDelegate = self

        present(photoPicker, animated: true, completion: nil)
    }
}