我正在尝试使用适用于Mac应用程序的网络摄像头获取用户照片,问题是当我使用AVCaptureDevice.devices()请求设备列表时,API返回0项,但是网络摄像头可在FaceTime和在Skype中。
@IBOutlet private weak var imageView: NSImageView!
let session = AVCaptureSession()
let devices = AVCaptureDevice.devices()
guard !devices.isEmpty else {
print(devices.count)
print("No device")
return
}
for device in devices {
print(device.localizedName)
}
if let device = devices.first {
do {
let input = try AVCaptureDeviceInput(device:device)
session.addInput(input)
let output = AVCaptureStillImageOutput()
output.outputSettings = [AVVideoCodecKey: AVVideoCodecType.jpeg]
session.addOutput(output)
if let connection = output.connection(with: AVMediaType.video) {
output.captureStillImageAsynchronously(from: connection) { (returnedBuffer, error) in
guard error == nil else {
print(error!.localizedDescription)
return
}
guard let buffer = returnedBuffer else {
return
}
guard let data = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(buffer) else {
return
}
if let image = NSImage(data: data) {
DispatchQueue.main.async {
self.imageView.image = image
}
}
}
}
} catch {
print(error.localizedDescription)
}
}
}