Skip to content

Instantly share code, notes, and snippets.

@vknabel
Last active August 29, 2015 14:24
Show Gist options
  • Save vknabel/71b7ea8d0ff833d22143 to your computer and use it in GitHub Desktop.
Save vknabel/71b7ea8d0ff833d22143 to your computer and use it in GitHub Desktop.
import AVFoundation
public typealias AVMediaType = String
public extension AVCaptureDevice {
public class func device(mediaType: AVMediaType, position: AVCaptureDevicePosition) -> AVCaptureDevice? {
let all = AVCaptureDevice.devicesWithMediaType(mediaType) as! [AVCaptureDevice]
for dev in all {
if dev.position == position {
return dev
}
}
return all.first
}
public func lockedConfiguration(var error: NSErrorPointer, operation: () -> ()) -> Bool {
// TODO: Swift 2.0 reimplement
if self.lockForConfiguration(nil) {
operation()
self.unlockForConfiguration()
return true
}
return false
}
public func focusWithMode(focusMode: AVCaptureFocusMode, exposeWithMode exposureMode: AVCaptureExposureMode, atDevicePoint point: CGPoint, monitorSubjectAreaChange: Bool) {
var error: NSError?
self.lockedConfiguration(&error) {
if self.focusPointOfInterestSupported && self.isFocusModeSupported(focusMode) {
self.focusMode = focusMode
self.focusPointOfInterest = point
}
if self.exposurePointOfInterestSupported && self.isExposureModeSupported(exposureMode) {
self.exposureMode = exposureMode
self.exposurePointOfInterest = point
}
self.subjectAreaChangeMonitoringEnabled = monitorSubjectAreaChange
}
if let error = error {
print("-AVCaptureDevice.focusWithMode(_:, exposeWithMode:, atDevicePoint:, monitorSubjectAreaChange:) \(error)")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment