使用AVCaptureSession以编程方式捕获最大分辨率图像

时间:2016-01-04 01:34:36

标签: ios iphone swift camera avfoundation

我的目标是使用AVCaptureSession以编程方式锁定焦点,捕获一张图像,激活闪光灯,然后在延迟一段时间后拍摄第二张图像。

我已设法使用AVCaptureSession实例和AVCaptureStillImageOutput使捕获工作正常。但是,我在拨打captureStillImageAsynchronouslyFromConnection(_:completionHandler:)时获得的图像是1920 x 1080,而不是iPhone 6S相机能够拍摄的完整1200万像素图像。

这是我的捕捉功能:

func captureImageFromStream(completion: (result: UIImage) -> Void)
{
    if let stillOutput = self.stillImageOutput {
        var videoConnection : AVCaptureConnection?
        for connection in stillOutput.connections {
            for port in connection.inputPorts! {
                if port.mediaType == AVMediaTypeVideo {
                    videoConnection = connection as? AVCaptureConnection
                    break
                }
            }
            if videoConnection != nil {
                break
            }
        }
        if videoConnection != nil {
            stillOutput.captureStillImageAsynchronouslyFromConnection(videoConnection) {
                (imageDataSampleBuffer, error) -> Void in

                if error == nil {
                    let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(imageDataSampleBuffer)

                    if let image = UIImage(data: imageData) {
                        completion(result: image)
                    }
                }
                else {
                    NSLog("ImageCapture Error: \(error)")
                }
            }
        }
    }
}

我应该做些什么修改来捕捉我正在寻找的图像?我是斯威夫特的新手,所以请原谅我所犯的任何初学者错误。

1 个答案:

答案 0 :(得分:2)

addOutput stillImageOutputstartRunning captureSession.sessionPreset = AVCaptureSessionPresetPhoto 之前,您需要将捕获会话预设设置为照片:

{{1}}
相关问题