在OS X(Yosemite)上使用iSight摄像头

时间:2014-08-22 09:08:51

标签: macos avfoundation osx-yosemite

我正试图从iSight拍摄一张照片。设置AVCaptureDevice和AVCaptureSession后 - 调用showCamera函数只会导致iSight摄像头打开然后关闭。

@IBAction func showCamera(sender: AnyObject) {
    var errorPointer:NSErrorPointer = nil

    let device = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
    let input = AVCaptureDeviceInput(device:device, error:errorPointer)
    let output = AVCaptureStillImageOutput()

    output.outputSettings = [kCVPixelBufferPixelFormatTypeKey : k32BGRAPixelFormat]

    let captureSession = AVCaptureSession()

    captureSession.sessionPreset = AVCaptureSessionPresetPhoto
    captureSession.addInput(input)
    captureSession.addOutput(output)
    captureSession.startRunning()

    let connection = output.connectionWithMediaType(AVMediaTypeVideo)

    output.captureStillImageAsynchronouslyFromConnection(connection, completionHandler: {(sampleBuffer, error) in
        if sampleBuffer != nil {
            let imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer)

            if (imageBuffer != nil) {
                let rep = NSCIImageRep(CIImage:CIImage(CVImageBuffer:imageBuffer))
                let image = NSImage(size:rep.size)
                image.addRepresentation(rep)
                self.imageView.image = image // Try to see the image
            }
        }
    })
}

关于什么可能出错的任何建议?谢谢!

0 个答案:

没有答案