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

How to use in UIImagePickerController #51

Open
StevenSorial opened this issue Jun 30, 2021 · 0 comments
Open

How to use in UIImagePickerController #51

StevenSorial opened this issue Jun 30, 2021 · 0 comments

Comments

@StevenSorial
Copy link

Trying to add a publisher to UIImagePickerController using a DelegateProxy

My code:

extension UIImagePickerController {
  var didFinishPickingMedia: AnyPublisher<[UIImagePickerController.InfoKey: Any], Never> {
    let didPickSelector =
      #selector(UIImagePickerControllerDelegate.imagePickerController(_:didFinishPickingMediaWithInfo:))

    return delegateProxy
      .interceptSelectorPublisher(didPickSelector)
      .map { $0[1] as! [UIImagePickerController.InfoKey: Any] }
      .eraseToAnyPublisher()
  }

  var didCancel: AnyPublisher<Void, Never> {
    let didCancelSelector = #selector(UIImagePickerControllerDelegate.imagePickerControllerDidCancel(_:))
    return delegateProxy
      .interceptSelectorPublisher(didCancelSelector)
      .map { _ in () }
      .eraseToAnyPublisher()
  }

  private var delegateProxy: UIImagePickerControllerDelegateProxy {
      .createDelegateProxy(for: self)
  }
}

private class UIImagePickerControllerDelegateProxy: DelegateProxy,
                                                    UIImagePickerControllerDelegate,
                                                    UINavigationControllerDelegate,
                                                    DelegateProxyType {
  func setDelegate(to object: UIImagePickerController) {
    object.delegate = self
  }
}

Works well on devices but crashes when presenting the controller on a simulator with 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'.

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

1 participant