RxMediaPicker — picking photos and videos the cool kids’ way!
If you ever used UIImagePickerController in your iOS apps and you didn’t like the experience, there are probably a few reasons that explain it: it gets a little verbose and difficult to reuse, you need to add some boilerplate to your screens and its interface is a little messy with several properties meant to be used in different situations. If that’s the case, you should try 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 photos, etc. And, at the same time, it attempts to provide a solution to ease all the aforementioned issues, offering the following features:
- RxSwift based 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;
- Separates video operations from photos operations;
- Easy to integrate and reuse across your app.
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, the same being available for photos as well.
Record video:
func recordVideo(device: UIImagePickerControllerCameraDevice = .Rear, quality: UIImagePickerControllerQualityType = .TypeMedium, maximumDuration: NSTimeInterval = 600, editable: Bool = false) -> Observable<NSURL>
Pick video:
func selectVideo(source: UIImagePickerControllerSourceType = .PhotoLibrary, maximumDuration: NSTimeInterval = 600, editable: Bool = false) -> Observable<NSURL>
Take photo:
func takePhoto(device: UIImagePickerControllerCameraDevice = .Rear, flashMode: UIImagePickerControllerCameraFlashMode = .Auto, editable: Bool = false) -> Observable<(UIImage, UIImage?)>
Pick image:
func selectImage(source: UIImagePickerControllerSourceType = .PhotoLibrary, editable: Bool = false) -> Observable<(UIImage, UIImage?)>
If you’re familiar with RxSwift already, using the available operations should be straightforward: for videos you get an Observable which gives you back the video URL; for photos you get an Observable which gives you back a tuple containing the original photo and an optional edited photo (in case you made any edits).
An example recording a video would look as simple as this:
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")
})
.addDisposableTo(disposeBag)
}
For a sample app have a look the repository at https://github.com/ruipfcosta/RxMediaPicker. Bug reports and pull requests are welcome!
If you have any questions get in touch on Twitter @ruipfcosta!