You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
50 lines
1.6 KiB
50 lines
1.6 KiB
// |
|
// 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<UIImagePickerController, |
|
UIImagePickerControllerDelegate & UINavigationControllerDelegate > { |
|
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<String, AnyObject>.self, a[1]) |
|
}) |
|
} |
|
|
|
//图片取消选择代理方法的封装 |
|
public var didCancel: Observable<()> { |
|
return pickerDelegate |
|
.methodInvoked(#selector(UIImagePickerControllerDelegate |
|
.imagePickerControllerDidCancel(_:))) |
|
.map {_ in () } |
|
} |
|
} |
|
|
|
//转类型的函数(转换失败后,会发出Error) |
|
fileprivate func castOrThrow<T>(_ resultType: T.Type, _ object: Any) throws -> T { |
|
guard let returnValue = object as? T else { |
|
throw RxCocoaError.castingError(object: object, targetType: resultType) |
|
} |
|
return returnValue |
|
} |
|
|
|
|