swift RxSwiftを使い,的UIImagePickerControllerをラップしたクラス。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了swift RxSwiftを使い,的UIImagePickerControllerをラップしたクラス。相关的知识,希望对你有一定的参考价值。

import Foundation
import UIKit
import Photos

import RxSwift
import RxCocoa

final class RxImagePicker: NSObject {
    
    private let didFinishPickingMediaSubject: PublishSubject<UIImage> = PublishSubject<UIImage>()
    private let didCancelSubject: PublishSubject<Void> = PublishSubject<Void>()
    
    public var didFinishPickingMedia: Observable<UIImage> { return didFinishPickingMediaSubject.asObserver() }
    public var didCancel: Observable<Void> { return didCancelSubject.asObserver() }

    public func selectImage(viewController fromViewController: UIViewController, tintColor: UIColor? = nil, allowEditing: Bool = false) {
        checkPermission()
        
        let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
        let cameraAction = UIAlertAction(title: "写真を撮る", style: .default) { (UIAlertAction) -> Void in
            self.selectFromCamera(fromViewController: fromViewController, allowEditing: allowEditing)
        }
        
        let libraryAction = UIAlertAction(title: "アルバムから選ぶ", style: .default) { (UIAlertAction) -> Void in
            self.selectFromLibrary(fromViewController: fromViewController, allowEditing: allowEditing)
        }
        
        let cancelAction = UIAlertAction(title: "キャンセル", style: .cancel) { (UIAlertAction) -> Void in
            fromViewController.dismiss(animated: true, completion: nil)
        }
        
        alertController.view.tintColor = tintColor
        alertController.addAction(cameraAction)
        alertController.addAction(libraryAction)
        alertController.addAction(cancelAction)
        fromViewController.present(alertController, animated: true, completion: nil)
    }
    
    func checkPermission() {
        let photoAuthorizationStatus = PHPhotoLibrary.authorizationStatus()
        switch photoAuthorizationStatus {
        case .authorized:
            print("Access is granted by user")
        case .notDetermined:
            PHPhotoLibrary.requestAuthorization({
                (newStatus) in
                print("status is \(newStatus)")
                if newStatus ==  PHAuthorizationStatus.authorized {
                    /* do stuff here */
                    print("success")
                }
            })
            print("It is not determined until now")
        case .restricted:
            // same same
            print("User do not have access to photo album.")
        case .denied:
            // same same
            print("User has denied the permission.")
        }
    }
    
    private func selectFromCamera(fromViewController: UIViewController, allowEditing: Bool) {
        if UIImagePickerController.isSourceTypeAvailable(.camera) {
            let imagePickerController = UIImagePickerController()
            imagePickerController.delegate = self
            imagePickerController.sourceType = UIImagePickerControllerSourceType.camera
            imagePickerController.allowsEditing = allowEditing
            fromViewController.present(imagePickerController, animated: true, completion: nil)
        } else {
            print("カメラ許可をしていない時の処理")
        }
    }
    
    private func selectFromLibrary(fromViewController: UIViewController, allowEditing: Bool) {
        if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
            let imagePickerController = UIImagePickerController()
            imagePickerController.delegate = self
            imagePickerController.sourceType = UIImagePickerControllerSourceType.photoLibrary
            imagePickerController.allowsEditing = allowEditing
            fromViewController.present(imagePickerController, animated: true, completion: nil)
        } else {
            print("カメラロール許可をしていない時の処理")
        }
    }
}

extension RxImagePicker: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    
    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        didCancelSubject.onNext(())
    }
    
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        if let image = info[UIImagePickerControllerEditedImage] as? UIImage {
            didFinishPickingMediaSubject.onNext(image)
        }
        
        if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
           didFinishPickingMediaSubject.onNext(image)
        }
        
        picker.dismiss(animated: true, completion: nil)
    }
}

以上是关于swift RxSwiftを使い,的UIImagePickerControllerをラップしたクラス。的主要内容,如果未能解决你的问题,请参考以下文章

css GoogleFont betaを使い,CSSでNoto Sans日语を使用する

swift ExpressibleByStringLiteralの使い所

swift 适用于iOS的SearchViewController 11.启用大标题。谢谢:【Swift】UISearchControllerの使い方。検索结果用ビューコントローラーを呼び出す。はじは

RXSwift的一些基本交互(OC,Swift,RXSwift对比)

RxSwift

Swift  之 RxSwift