【发布时间】:2015-07-07 23:16:13
【问题描述】:
我创建了一个 AVCaptureSession 来捕获视频输出并通过 UIView 将其显示给用户。现在我希望能够单击一个按钮(takePhoto 方法)并在 UIImageView 中显示会话中的图像。我试图遍历每个设备连接并尝试保存输出,但没有奏效。我的代码如下
let captureSession = AVCaptureSession()
var stillImageOutput: AVCaptureStillImageOutput!
@IBOutlet var imageView: UIImageView!
@IBOutlet var cameraView: UIView!
// If we find a device we'll store it here for later use
var captureDevice : AVCaptureDevice?
override func viewDidLoad() {
// Do any additional setup after loading the view, typically from a nib.
super.viewDidLoad()
println("I AM AT THE CAMERA")
captureSession.sessionPreset = AVCaptureSessionPresetLow
self.captureDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
if(captureDevice != nil){
beginSession()
}
}
func beginSession() {
self.stillImageOutput = AVCaptureStillImageOutput()
self.captureSession.addOutput(self.stillImageOutput)
var err : NSError? = nil
self.captureSession.addInput(AVCaptureDeviceInput(device: self.captureDevice, error: &err))
if err != nil {
println("error: \(err?.localizedDescription)")
}
var previewLayer = AVCaptureVideoPreviewLayer(session: self.captureSession)
self.cameraView.layer.addSublayer(previewLayer)
previewLayer?.frame = self.cameraView.layer.frame
captureSession.startRunning()
}
@IBAction func takePhoto(sender: UIButton) {
self.stillImageOutput.captureStillImageAsynchronouslyFromConnection(self.stillImageOutput.connectionWithMediaType(AVMediaTypeVideo)) { (buffer:CMSampleBuffer!, error:NSError!) -> Void in
var image = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(buffer)
var data_image = UIImage(data: image)
self.imageView.image = data_image
}
}
}
【问题讨论】:
-
现在出现这个错误
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** Cannot add output <AVCaptureStillImageOutput: 0x1742221c0> to capture session <AVCaptureSession: 0x17000ae70 [AVCaptureSessionPresetLow]> <AVCaptureDeviceInput: 0x174221840 [Back Camera]> -> <AVCaptureVideoPreviewLayer: 0x1742217e0> <AVCaptureDeviceInput: 0x174221840 [Back Camera]> -> <AVCaptureStillImageOutput: 0x174221da0> because more than one output of the same type is unsupported.' -
首先取出以下行: captureSession.addOutput(self.stillImageOutput) 并查看添加位置。这应该可以解决错误。
-
@user3353890 我修复了错误(我更新了上面的代码),但现在我只获得了用于预览的静态图像,而不是来自相机的视频......?
-
静态图片是什么意思?
-
@user3353890 cameraview 不显示相机正在捕获的视频,而只是设置为在 capturesession 开始运行时相机首先捕获的任何静态图像。
标签: ios swift avcapturesession