Last active
August 6, 2016 18:22
-
-
Save chuganzy/4e8a3eef8989b3d5c039 to your computer and use it in GitHub Desktop.
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 AVFoundation | |
import RxSwift | |
import RxCocoa | |
private let associatedkey = UnsafePointer<Void>(malloc(1)) | |
class RxCaptureMetadataOutputObjectsDelegateProxy: DelegateProxy, DelegateProxyType, AVCaptureMetadataOutputObjectsDelegate { | |
static func setCurrentDelegate(delegate: AnyObject?, toObject object: AnyObject) { | |
(object as! AVCaptureMetadataOutput) | |
.setMetadataObjectsDelegate(delegate.map { $0 as! AVCaptureMetadataOutputObjectsDelegate }, | |
queue: objc_getAssociatedObject(object, associatedkey) as! dispatch_queue_t) | |
} | |
static func currentDelegateFor(object: AnyObject) -> AnyObject? { | |
return (object as! AVCaptureMetadataOutput).metadataObjectsDelegate | |
} | |
func observeDidOutputMetadataObjects() -> Observable<([AnyObject], AVCaptureConnection)> { | |
return observe(#selector(AVCaptureMetadataOutputObjectsDelegate.captureOutput(_:didOutputMetadataObjects:fromConnection:))) | |
.map { ($0[1] as! [AnyObject], $0[2] as! AVCaptureConnection) } | |
} | |
} | |
extension AVCaptureMetadataOutput { | |
func rx_metadataObjectsDelegate(queue: dispatch_queue_t) -> RxCaptureMetadataOutputObjectsDelegateProxy { | |
objc_setAssociatedObject(self, associatedkey, queue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) | |
return RxCaptureMetadataOutputObjectsDelegateProxy.proxyForObject(self) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment