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.
 
 
 

82 lines
3.2 KiB

//
// UIImagePickerController+RxCreate.swift
// RxExample
//
// Created by Krunoslav Zaher on 1/10/16.
// Copyright © 2016 Krunoslav Zaher. All rights reserved.
//
import UIKit
import RxSwift
import RxCocoa
//
func dismissViewController(_ viewController: UIViewController, animated: Bool) {
if viewController.isBeingDismissed || viewController.isBeingPresented {
DispatchQueue.main.async {
dismissViewController(viewController, animated: animated)
}
return
}
if viewController.presentingViewController != nil {
viewController.dismiss(animated: animated, completion: nil)
}
}
//UIImagePickerControllerRx
extension Reactive where Base: UIImagePickerController {
//
static func createWithParent(_ parent: UIViewController?,
animated: Bool = true,
configureImagePicker: @escaping (UIImagePickerController) throws -> () = { x in })
-> Observable<UIImagePickerController> {
//
return Observable.create { [weak parent] observer in
//
let imagePicker = UIImagePickerController()
//.completed
let dismissDisposable = Observable.merge(
imagePicker.rx.didFinishPickingMediaWithInfo.map{_ in ()},
imagePicker.rx.didCancel
)
.subscribe(onNext: { _ in
observer.on(.completed)
})
//.error
do {
try configureImagePicker(imagePicker)
}
catch let error {
observer.on(.error(error))
return Disposables.create()
}
//parent.completed
guard let parent = parent else {
observer.on(.completed)
return Disposables.create()
}
// IOS11
if #available(iOS 11.0, *) {
UIScrollView.appearance().contentInsetAdjustmentBehavior = .automatic
}
//
parent.present(imagePicker, animated: animated, completion: nil)
//.next
observer.on(.next(imagePicker))
//退
return Disposables.create(dismissDisposable, Disposables.create {
dismissViewController(imagePicker, animated: animated)
if #available(iOS 11.0, *) {
UIScrollView.appearance().contentInsetAdjustmentBehavior = .never
}
})
}
}
}