Skip to content

Instantly share code, notes, and snippets.

@hasanadil
Created March 9, 2015 20:43
Show Gist options
  • Save hasanadil/3c40e35ebac22267d670 to your computer and use it in GitHub Desktop.
Save hasanadil/3c40e35ebac22267d670 to your computer and use it in GitHub Desktop.
let camera = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
var error : NSError? = nil
let cameraInput : AVCaptureDeviceInput? = AVCaptureDeviceInput.deviceInputWithDevice(camera, error: &error) as? AVCaptureDeviceInput
self.session.addInput(cameraInput)
if error != nil || cameraInput == nil {
//Awesome error handling :D
println(error!.localizedDescription)
}
else {
let cameraOutput = AVCaptureMetadataOutput()
cameraOutput.setMetadataObjectsDelegate(self, queue: dispatch_get_main_queue())
self.session.addOutput(cameraOutput)
cameraOutput.metadataObjectTypes = cameraOutput.availableMetadataObjectTypes
self.previewLayer = AVCaptureVideoPreviewLayer.layerWithSession(self.session) as AVCaptureVideoPreviewLayer
self.previewLayer.frame = view.bounds
self.previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
view.layer.addSublayer(self.previewLayer)
self.session.startRunning()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment