ReactiveX / RxSwift

Reactive Programming in Swift
MIT License
24.41k stars 4.17k forks source link

UIimagePickerController with RxSwift #376

Closed kbala-ami closed 8 years ago

kbala-ami commented 8 years ago

Anyone suggest a best way to use UIimagePickerController for photo capturing?

Thanks a lot!

sergdort commented 8 years ago

I guess you can create your own DelegateProxy this post could be helpful

kbala-ami commented 8 years ago

Thanks a lot, will look into :)

kzaher commented 8 years ago

Yeah, we'll also try to find time for some examples maybe.

Haven't used UIImagePickerController for a couple of years, so I'll need to check first all of the new features that were added meanwhile, and figure out is there some nice generic stateless way to wrap it :)

sergdort commented 8 years ago

Hi, @kzaher do you think this is something that we should have in RxCocoa ?

kzaher commented 8 years ago

I think this depends on how nicely can this be wrapped :)

The same is valid for UIAlertViewController. They would be useful, but it is awkward to wrap them in such a way that supports all future changes and all usage scenarios.

That's why we have just an example how you can use UIAlertViewController in example app, and not a real hardcoded public API inside RxCocoa.

https://developer.apple.com/library/ios/samplecode/PhotoPicker/Listings/PhotoPicker_APLViewController_m.html#//apple_ref/doc/uid/DTS40010196-PhotoPicker_APLViewController_m-DontLinkElementID_6

The dismissal process is also little problematic [self dismissViewControllerAnimated:YES completion:NULL]; since it can be animated or not. We could figure that out when observable sequence is created, but that decision could be deferred until the end, and changed depending on picking result.

I'll need some time to figure out the optimal solution :)

Right now it looks to me that we can probably wrap UIImagePickerDelegate inside RxCocoa, and create generic wrapping example inside example project.

This kind of reminds me of Alamofire wrapping :/

¯_(ツ)_/¯

sergdort commented 8 years ago

@kzaher if you don't mind, I can spend some time and try to wrap it into something kind of RxImagePickerDelegateProxy :)

kbala-ami commented 8 years ago

Yep, good if someone make an example work for Camera and Photo Manipulation using RxSwift. Me too can assist if you like :)

sergdort commented 8 years ago

Hi, guys. Do you think UIImagePickerController+Rx should have only two var ?

Kind of :

rx_didFinishPickingMediaWithInfo: Observable<[String : AnyObject]>

in this case user will need to use map operator

rx_didCancel: Observable<Void>

Or it should also have something like:

rx_originalImage: Observable<UIImage?>
rx_editedImage: Observable<UIImage?>
kbala-ami commented 8 years ago

Yes, It has 2 responses, Either captured or Cancelled. Hope you finished the example, let me look into now.

Is there any possible to bring Square viewfinder to capture square images?

sergdort commented 8 years ago

Hi, @kbala looks like it has been merged into develop branch #387, also there is an example:)

kbala-ami commented 8 years ago

Yes, I noticed that, Implementing that into my project :)

kbala-ami commented 8 years ago

Hi @sergdort, it works good as expected :+1:

I know didCancel is Void observable, but the below code not called, please advise

 _ = imagePickerController
        .rx_didCancel
        .map{ _ in
            print("cancelled")

    }
sergdort commented 8 years ago

Hi, @kbala as far as I understand map operator called only if there are any subscribers.

If you will do something like this:

imagePickerController.rx_didCancel
                .map { _ in
                    print("cancel");
                }.subscribeNext({ (_) -> Void in

                }).addDisposableTo(disposeBag)

It will get called

kbala-ami commented 8 years ago

Oh really, thanks a lot, I know I am still not having good understanding with Rx.

kzaher commented 8 years ago

I think we can probably close this one. I've polished UIImagePicker example inside Example app a little and released 2.1.0.

Hope it's clearer now.

kbala-ami commented 8 years ago

Hi @kzaher Now it looks simple. Cancel button not working, how to enable rx_didCancel? Please help.

sergdort commented 8 years ago

Hi, @kbala are you talking about example app?

kbala-ami commented 8 years ago

Yes, but also I implement the RxUIImagePickerController in my project, the cancel button not functioning. it should work by default.

Dwar3xwar commented 8 years ago

I think the delegate for UIImagePickerController+Rx.swiftdoes not dismiss the image picker because the parent view controller has to dismiss the image picker.

Current Implementation:

public var rx_didCancel: Observable<()> {
            return rx_delegate
                .observe("imagePickerControllerDidCancel:")
                .map {_ in () }
        }

From the official docs: Your delegate’s implementation of this method should dismiss the picker view by calling the dismissModalViewControllerAnimated: method of the parent view controller.

So you might have to manually call dismissViewControllerAnimated: from your parent view controllers.

Try this:

imagePickerController.rx_didCancel
                .map { _ in
                    print("cancel");
                }.subscribeNext{ [unowned self] _ in
                   self.dismissViewControllerAnimated(true, completion: nil)
                }.addDisposableTo(disposeBag)
sergdort commented 8 years ago

Please take a look on example app, in develop branch, I guess the changes has been merged recently