Created
December 27, 2016 03:10
-
-
Save hungdv136/89e2b68c3ee41340014022b1340919cf to your computer and use it in GitHub Desktop.
Using PublishSubject
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.swift | |
// RxSubject | |
// | |
// Created by Hung Dinh Van on 12/27/16. | |
// Copyright © 2016 ChuCuoi. All rights reserved. | |
// | |
import UIKit | |
import RxSwift | |
import RxCocoa | |
class ViewController: UIViewController { | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
let imagePicker = UIImagePickerController() | |
imagePicker.delegate = self | |
button.rx.tap.subscribe(onNext: { [unowned self] in | |
imagePicker.allowsEditing = false | |
imagePicker.sourceType = .photoLibrary | |
self.present(imagePicker, animated: true, completion: nil) | |
}).addDisposableTo(disposeBag) | |
didFinishPickingSubject.subscribe(onNext: { [unowned self] info in | |
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage { | |
self.imageView.contentMode = .scaleAspectFit | |
self.imageView.image = pickedImage | |
} | |
self.dismiss(animated: true, completion: nil) | |
}).addDisposableTo(disposeBag) | |
} | |
@IBOutlet fileprivate weak var imageView: UIImageView! | |
@IBOutlet fileprivate weak var button: UIButton! | |
fileprivate let disposeBag = DisposeBag() | |
fileprivate let didFinishPickingSubject = PublishSubject<[String : Any]>() | |
} | |
extension ViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate { | |
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { | |
didFinishPickingSubject.onNext(info) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment