RxSwift官方实例六(UIImagePickerContro

2020-11-05  本文已影响0人  酒茶白开水

代码下载

UIImagePickerController

搭建UI

构建如下UI:


定位UI

设置按钮的是否可用:

        cameraButton.isEnabled = UIImagePickerController.isSourceTypeAvailable(.camera)

UIImagePickerControllerDelegate的Rx实现

UIImagePickerController的代理对象需要遵守UIImagePickerControllerDelegateUINavigationControllerDelegate两个协议的:

weak open var delegate: (UIImagePickerControllerDelegate & UINavigationControllerDelegate)?

由于RxCocoa已经实现UINavigationControllerDelegateRxNavigationControllerDelegateProxy,没有实现UIImagePickerControllerDelegate,所以需要我们自己实现。

构建RxImagePickerDelegateProxy

从前面Delegate章节的DelegateProxyType讲到,拥有delegates的视图可能是继承的,RxCocoa需要创建的那些代理也是继承的。

由于UIImagePickerController继承自UINavigationController,定义RxImagePickerDelegateProxy类继承RxNavigationControllerDelegateProxy基类,遵守UIImagePickerControllerDelegate协议:

class RxImagePickerDelegateProxy: RxNavigationControllerDelegateProxy, UIImagePickerControllerDelegate {
    public init(imagePicker: UIImagePickerController) {
        super.init(navigationController: imagePicker)
    }
}

首先由于继承自RxNavigationControllerDelegateProxy,所以实现了DelegateProxyType协议中定义的函数。

UIImagePickerControllerDelegate协议中定义的函数都没有实现,最终会走消息转发的方式实现Rx序列发送元素。

扩展Reactive

首先定义一个func dismissViewController(viewController: UIViewController, animated: Bool)函数,用来安全有效地关闭模态视图,方便后面使用:

func dismissViewController(viewController: UIViewController, animated: Bool) {
    /// 是否有控制器在进程中没有显示或消失
    if viewController.isBeingPresented || viewController.isBeingDismissed {
        DispatchQueue.main.async {// 异步递归调用
            dismissViewController(viewController: viewController, animated: animated)
        }
    } else if viewController.presentingViewController != nil {
        viewController.dismiss(animated: animated, completion: nil)
    }
}

定义一个private func castOrThrow<T>(resultType: T.Type, object: Any) throws -> T函数,用于将Any类型数据转换为特定类型,方便后面使用:

private func castOrThrow<T>(resultType: T.Type, object: Any) throws -> T {
    guard let resultValue = object as? T else {
        throw RxCocoaError.castingError(object: object, targetType: resultType)
    }
    
    return resultValue
}

为了方便使用,基于UIImagePickerController扩展Reactive:

extension Reactive where Base: UIImagePickerController {
    public var didCancel: Observable<()> {
        return delegate.methodInvoked(#selector(UIImagePickerControllerDelegate.imagePickerControllerDidCancel(_:))).map { (_) -> () in }
    }
    public var didFinishPickingMediaWithInfo: Observable<[UIImagePickerController.InfoKey: AnyObject]> {
        return delegate.methodInvoked(#selector(UIImagePickerControllerDelegate.imagePickerController(_:didFinishPickingMediaWithInfo:))).map { (a) in
            return try castOrThrow(resultType: Dictionary<UIImagePickerController.InfoKey, AnyObject>.self, object: a[1])
        }
    }
    
    /// 创建图片选择控制器Observable
    /// - Parameters:
    ///   - parent: 父控制器
    ///   - animated: 动画
    ///   - configureImagePicker: 配置闭包
    static func createWithParent(parent: UIViewController?, animated: Bool = true, configureImagePicker: @escaping (UIImagePickerController) throws -> Void) -> Observable<UIImagePickerController> {
        return Observable.create { [weak parent] (observer) -> Disposable in
            let imagePicker = UIImagePickerController()
            // 取消操作
            let dismissDisposable = imagePicker.rx.didCancel.subscribe(onNext: { [weak imagePicker] (_) in
                guard let imagePicker = imagePicker else {
                    return
                }
                
                dismissViewController(viewController: imagePicker, animated: animated)
            })
            
            // 处理配置闭包
            do {
                try configureImagePicker(imagePicker)
            } catch let error {
                observer.onError(error)
                return Disposables.create()
            }
            
            guard let parent = parent else {
                observer.onCompleted()
                return Disposables.create()
            }
            parent.present(imagePicker, animated: animated, completion: nil)
            observer.on(.next(imagePicker))
            
            return Disposables.create(dismissDisposable, Disposables.create {
                dismissViewController(viewController: imagePicker, animated: animated)
            })
        }
    }
}

代码分析:

绑定UI

在iOS中使用相机、相册资源,需要在info.plist文件中配置相机、相册权限询问框的描述,如下所示:
[图片上传失败...(image-f2b960-1604572278262)]

因为在使用rx.*之前(例如appDidFinishLaunching),在registerKnownImplementations或程序的其他一些地方使用执行注册Rx实现的代理。

然而RxImagePickerDelegateProxy没有实现registerKnownImplementations函数,也没有再父类中注册,所以选择在程序启动时的appDidFinishLaunching中注册RxImagePickerDelegateProxy

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        
        RxImagePickerDelegateProxy.register { (p) -> RxImagePickerDelegateProxy in
            return RxImagePickerDelegateProxy(imagePicker: p)
        }
        
        return true
    }

数据绑定

        /// 拍照是否可用
        cameraButton.isEnabled = UIImagePickerController.isSourceTypeAvailable(.camera)
        
        cameraButton.rx.tap.flatMapLatest { [weak self] _ in
            return UIImagePickerController.rx.createWithParent(parent: self, animated: true) { (picker) in
                picker.sourceType = .camera
                picker.allowsEditing = false
            }.flatMap { $0.rx.didFinishPickingMediaWithInfo }.take(1)
        }.map { $0[.originalImage] as? UIImage }.bind(to: imageView.rx.image).disposed(by: bag)
        
        galleryButton.rx.tap.flatMapLatest { [weak self] (_) in
            return UIImagePickerController.rx.createWithParent(parent: self) { (picker) in
                picker.sourceType = .photoLibrary
                picker.allowsEditing = false
                }.flatMap { $0.rx.didFinishPickingMediaWithInfo }.take(1)
        }.map { $0[.originalImage] as? UIImage }.bind(to: imageView.rx.image).disposed(by: bag)
        
        cropButton.rx.tap.flatMapLatest { [weak self] (_) in
            return UIImagePickerController.rx.createWithParent(parent: self) { (picker) in
                picker.sourceType = .photoLibrary
                picker.allowsEditing = true
                }.flatMap { $0.rx.didFinishPickingMediaWithInfo }.take(1)
        }.map { $0[.editedImage] as? UIImage }.bind(to: imageView.rx.image).disposed(by: bag)
上一篇下一篇

猜你喜欢

热点阅读