我正在尝试从 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
}
}
})
}
关于可能出错的任何建议?谢谢!