Created
April 10, 2017 12:44
-
-
Save huguesbr/d592b4bab52673245d8d9128d51483be to your computer and use it in GitHub Desktop.
Photo Writer - Sample Rx wrapper around the global function UIImageWriteToSavedPhotosAlbum
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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