Git Product home page Git Product logo

rxmediapicker's Introduction

RxMediaPicker

RxMediaPicker is a RxSwift wrapper built around UIImagePickerController consisting in a simple interface for common actions like picking photos or videos stored on the device, recording a video or taking a photo.

Swift Version License Platform Carthage compatible

If you ever used UIImagePickerController you know how it can become quite verbose and introduce some unnecessary complexity, as you use the same class to do everything, from picking photos or videos from the library, recording videos, etc. At the same time, you have all these different properties and methods which can be used when you are dealing with photos, others for when you're dealing with videos, and others in all cases.

If you're interested, check the resulting article RxMediaPicker — picking photos and videos the cool kids’ way!

Note: As RxMediaPicker is still in its early days, the interface may change in future versions.

Features

  • Reactive wrapper built around UIImagePickerController.
  • Provides an interface for common operations like picking photos from the library, recording videos, etc.
  • Handles edited videos properly - something UIImagePickerController doesn't do for you!
  • Decouples the typical UIImagePickerController boilerplate from your code.
  • Reduces the complexity when compared to UIImagePickerController.
  • Easy to integrate and reuse accross your app.

Example

For a more complete example please check the demo app included (ideally run it on a device). This is how you would record a video using the camera:

import RxMediaPicker
import RxSwift

var picker: RxMediaPicker!
let disposeBag = DisposeBag()

override func viewDidLoad() {
    super.viewDidLoad()
    picker = RxMediaPicker(delegate: self)
}

func recordVideo() {
    picker.recordVideo(maximumDuration: 10, editable: true)
        .observeOn(MainScheduler.instance)
        .subscribe(onNext: { url in
            // Do something with the video url obtained!
        }, onError: { error in
            print("Error occurred!")
        }, onCompleted: {
            print("Completed")
        }, onDisposed: {
            print("Disposed")
        })
        .disposed(by: disposeBag)
}

Usage

Available operations

Based on their names, the operations available on RxMediaPicker should be self-explanatory. You can record a video, or pick an existing one stored on the device, and the same thing happens for photos. The only thing to note here is that picking a video will get you the video url, and picking a photo will get you a tuple consisting in the original image and an optional edited image (if any edits were made).

func recordVideo(device: UIImagePickerController.CameraDevice = .rear, 
                 quality: UIImagePickerController.QualityType = .typeMedium, 
                 maximumDuration: TimeInterval = 600,
                 editable: Bool = false) -> Observable<URL>
func selectVideo(source: UIImagePickerController.SourceType = .photoLibrary, 
                 maximumDuration: TimeInterval = 600,
                 editable: Bool = false) -> Observable<URL>
func takePhoto(device: UIImagePickerController.CameraDevice = .rear, 
               flashMode: UIImagePickerController.CameraFlashMode = .auto, 
               editable: Bool = false) -> Observable<(UIImage, UIImage?)>
func selectImage(source: UIImagePickerController.SourceType = .photoLibrary, 
                 editable: Bool = false) -> Observable<(UIImage, UIImage?)>

RxMediaPickerDelegate

func present(picker: UIImagePickerController)
func dismiss(picker: UIImagePickerController) 

To be able to use RxMediaPicker you will need to adopt the protocol RxMediaPickerDelegate. This is required to indicate RxMediaPicker how the camera/photos picker should be presented. For example, you may want to present the photos library picker in a popover on the iPad, and use the entire screen on the iPhone.

Requirements

  • iOS 8.0+
  • Xcode 7.0+

Instalation

CocoaPods

CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:

$ gem install cocoapods

To integrate RxMediaPicker into your Xcode project using CocoaPods, include this in your Podfile:

platform :ios, '8.0'
use_frameworks!

pod 'RxMediaPicker'

Carthage

Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks.

You can install Carthage with Homebrew using the following command:

$ brew update
$ brew install carthage

To integrate RxMediaPicker into your Xcode project using Carthage, specify it in your Cartfile:

github "RxSwiftCommunity/RxMediaPicker" "master"

Run carthage to build the framework and drag the built RxMediaPicker.framework into your Xcode project.

Create a Package.swift file.

// swift-tools-version:5.0

import PackageDescription

let package = Package(
  name: "YOUR_PROJECT_NAME",
  dependencies: [
    .package(url: "https://github.com/RxSwiftCommunity/RxMediaPicker.git", from: "2.0.0")
  ],
  targets: [
    .target(name: "YOUR_PROJECT_NAME", dependencies: ["RxMediaPicker"])
  ]
)

Credits

Owned and maintained by Rui Costa (@ruipfcosta).

Contributing

Bug reports and pull requests are welcome.

License

RxMediaPicker is released under the MIT license. See LICENSE for details.

rxmediapicker's People

Contributors

andraskadar avatar andrewsb avatar cruisediary avatar engali94 avatar freak4pc avatar ruipfcosta avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

rxmediapicker's Issues

Cancel emits an error instead of a next event

I'm not sure if is it good to send an Error event when use cancels during picking an image.
Even more, my app crash when I press cancel, what is the reason?

fatal error: Binding error to variable: canceled: file /Users/adamsmaka/Dropbox/Projects/TakeActionInspire/Pods/RxCocoa/RxCocoa/RxCocoa.swift, line 154

screen shot 2017-05-26 at 1 43 16 pm

No edit photos will have a problem

func processPhoto(info: [String : AnyObject],
                  observer: AnyObserver<(UIImage, UIImage?)>) {

// guard let image = info[UIImagePickerControllerOriginalImage] as? UIImage,
// let editedImage = info[UIImagePickerControllerEditedImage] as? UIImage else {
// observer.on(.error(RxMediaPickerError.generalError))
// return
// }

    let image = info[UIImagePickerControllerOriginalImage] as! UIImage
    let editedImage = info[UIImagePickerControllerEditedImage] as? UIImage

    observer.on(.next(image, editedImage))
    observer.on(.completed)
}

Push '2.0.0' to Cocoapods

now I can only use

pod 'RxMediaPicker', :git => 'https://github.com/RxSwiftCommunity/RxMediaPicker.git', :tag => '2.0.0'

Objective-C version

there will be version for the objective C ?

I have worked with UIImagePickerController

Thanks

Add basic CI / Travis for Pull Requests

This repo could definitely use a basic Travis implementation so future PRs are guaranteed to not break the basic functionality.

Not sure will have time to do this soon but it's good to capture this being a need :)

Error when picking photo with editable: false

Hi, when I use function picker.selectImage(editable: true) everything is fine
But when I change editable to false, then after picking a picture I got error
Picker photo error: The operation couldn’t be completed. (RxMediaPicker.RxMediaPickerError error 0.)
Why does this happen?

Support for RxSwift 4.x

Hi,

I tried to integrate RxMediaPicker via CocoaPods, however, I came across this issue that current pod is based on RxSwift 3.x.x. Any idea how can we make it work with RxSwift 4.x.x? Thanks.

Update API: Observable -> Single

In #9 we talked about updating to the new RxSwift Single stream.

I'll take a shot at this next time I'm using the library, otherwise anyone else is more than welcome to make a PR 😄

Carthage --platform iOS command doesn't work

Hello, I tried to add the project through Carthage but doesn't work with the error below.

image

I tried with carthage update RxMediaPicker --platform iOS --cache-builds and without --cache-builds command as well, both not working.

I am using Xcode 12.1 and my cartfile is like below.

...
github "ReactiveX/RxSwift" ~> 5.1.0
github "RxSwiftCommunity/RxAlamofire" ~> 5.3
github "RxSwiftCommunity/RxDataSources" ~> 4.0
github "RxSwiftCommunity/RxAVFoundation"
github "RxSwiftCommunity/RxRealm" ~> 3.1
github "RxSwiftCommunity/RxMediaPicker" ~> 2.0
github "RxSwiftCommunity/RxGesture" ~> 3.0
github "realm/realm-cocoa" ~> 5.0

Thanks

Can't open the Image Picker for the second time

Hi, I can't open the ImagePicker for the second time.
After looking into the code, here are what I got:

  1. I got the observer will emit onError when the app did cancel the Image Picker, so it will emit onDisposed also.
    open func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        dismiss(picker)
        
        if let action = currentAction {
            switch action {
            case .photo(let observer): observer.on(.error(RxMediaPickerError.canceled))
            case .video(let observer, _): observer.on(.error(RxMediaPickerError.canceled))
            }
        }
    }
  1. I got the observer will emit onComplete when the app finishes pick the image, it will emit onDisposed also.
    func processPhoto(info: [UIImagePickerController.InfoKey: Any],
                      observer: AnyObserver<(UIImage, UIImage?)>) {
        guard let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage else {
            observer.on(.error(RxMediaPickerError.generalError))
            return
        }
        
        let editedImage = info[UIImagePickerController.InfoKey.editedImage] as? UIImage
        observer.onNext((image, editedImage))
        observer.onCompleted()
    }

That's why I can't open the ImagePicker for the second time. So, these are what I suggest:

  1. Only call dismiss(picker) inside func imagePickerControllerDidCancel(_ picker: UIImagePickerController)
    open func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        dismiss(picker)
    }
  1. Call onComplete after picking the image is not necessary
    func processPhoto(info: [UIImagePickerController.InfoKey: Any],
                      observer: AnyObserver<(UIImage, UIImage?)>) {
        guard let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage else {
            observer.on(.error(RxMediaPickerError.generalError))
            return
        }
        
        let editedImage = info[UIImagePickerController.InfoKey.editedImage] as? UIImage
        observer.onNext((image, editedImage))
    }

Get the info of a selected image

One question, is there a way to get the info[String: Any] picking an image from the library?

I need the info to get the URL and work with the fetched PHAsset, get the placeholder... yada, yada, yada... and selectImage function just give me the UIImage's.

takePhoto method doesn't take into account what is in info.plist

Hi, I am having issue because this method doesn't really take into account what is set in info.plist about Privacy - Camera usage. So, when I try to use camera, the system doesn't pop out with an alert about Camera usage description.

Here is the code:

func cameraAction() {
       if UIImagePickerController.isSourceTypeAvailable(.camera) {
           picker.takePhoto(editable: true).subscribe(onNext:{ [weak self] value in
               guard let `self` = self else{ return }
               if let editedImage = value.1{
                   self.viewModel.profileNewImageInput.accept(editedImage)
               }
               else{
                   self.viewModel.profileNewImageInput.accept(value.0)
               }
           }).disposed(by: dispose)
       }
   }

Am I supposed to do all checks by myself, or ?

CocoaPods - Can't install the latest version compatible with Swift 5

Since the last version of this pod that was published only supports much earlier versions of RxSwift, this pod can't be imported when using cocoapods and RxSwift ~> 5.1. It would be great if we could get this released so those of us who can't upgrade to SPM could use this nice framework.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.