Skip to content

Instantly share code, notes, and snippets.

@huguesbr
Created April 10, 2017 12:44
Show Gist options
  • Save huguesbr/d592b4bab52673245d8d9128d51483be to your computer and use it in GitHub Desktop.
Save huguesbr/d592b4bab52673245d8d9128d51483be to your computer and use it in GitHub Desktop.
Photo Writer - Sample Rx wrapper around the global function UIImageWriteToSavedPhotosAlbum
import Foundation
import UIKit
import RxSwift
class PhotoWriter: NSObject {
typealias Callback = (NSError?)->Void
let disposeBag = DisposeBag()
private var callback: Callback
init(callback: @escaping Callback) {
self.callback = callback
}
static func save(_ image: UIImage) -> Observable<Void> {
return Observable<Void>.create { (observer) -> Disposable in
let writer = PhotoWriter(callback: { error in
if let error = error {
observer.onError(error)
} else {
observer.onCompleted()
}
})
UIImageWriteToSavedPhotosAlbum(image, writer, #selector(PhotoWriter.image(_:didFinishSavingWithError:contextInfo:)), nil)
return Disposables.create()
}
}
@objc func image(_ image: UIImage, didFinishSavingWithError error: NSError?, contextInfo info: UnsafeRawPointer) {
callback(error)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment