// // UIImagePickerController+Rx.swift // RxExample // // Created by Segii Shulga on 1/4/16. // Copyright © 2016 Krunoslav Zaher. All rights reserved. // import RxSwift import RxCocoa import UIKit //图片选择控制器(UIImagePickerController)的Rx扩展 extension Reactive where Base: UIImagePickerController { //代理委托 public var pickerDelegate: DelegateProxy { return RxImagePickerDelegateProxy.proxy(for: base) } //图片选择完毕代理方法的封装 public var didFinishPickingMediaWithInfo: Observable<[String : AnyObject]> { return pickerDelegate .methodInvoked(#selector(UIImagePickerControllerDelegate .imagePickerController(_:didFinishPickingMediaWithInfo:))) .map({ (a) in return try castOrThrow(Dictionary.self, a[1]) }) } //图片取消选择代理方法的封装 public var didCancel: Observable<()> { return pickerDelegate .methodInvoked(#selector(UIImagePickerControllerDelegate .imagePickerControllerDidCancel(_:))) .map {_ in () } } } //转类型的函数(转换失败后,会发出Error) fileprivate func castOrThrow(_ resultType: T.Type, _ object: Any) throws -> T { guard let returnValue = object as? T else { throw RxCocoaError.castingError(object: object, targetType: resultType) } return returnValue }