使用相机拍摄的照片在 iOS w/ Swift 上非常暗

Pictures taken with the camera come out really dark on iOS w/ Swift

相机预览看起来很完美,但是当我拍一张照片并将其保存到相机胶卷时,照片变得非常暗。在这里尝试了一堆线程,但没有解决它。这是我的代码。

这设置了 camera/preview 并且工作正常:

 captureSession.sessionPreset = AVCaptureSessionPreset640x480

        let devices = AVCaptureDevice.devices()

        // Loop through all the capture devices on this phone
        for device in devices {
            // Make sure this particular device supports video
            if (device.hasMediaType(AVMediaTypeVideo)) {
                // Finally check the position and confirm we've got the back camera
                if(device.position == AVCaptureDevicePosition.Back) {
                    captureDevice = device as? AVCaptureDevice
                }
            }
        }

        if captureDevice != nil {
            beginSession()
        }

func beginSession() {
    var err : NSError? = nil

    if captureSession.canAddInput(AVCaptureDeviceInput(device: captureDevice, error: &err)) {
        captureSession.addInput(AVCaptureDeviceInput(device: captureDevice, error: &err))

        if err != nil {
            println("error: \(err?.localizedDescription)")
        }

        let previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
        self.view.layer.addSublayer(previewLayer)
        self.view.bringSubviewToFront(cameraButton)
        previewLayer?.frame = self.view.layer.frame

        // start camera
        captureSession.startRunning()

   }

拍照时调用:

@IBAction func photoTaken(sender: UIButton) {

    stillImageOutput.outputSettings = [AVVideoCodecKey: AVVideoCodecJPEG]
    if captureSession.canAddOutput(stillImageOutput) {
        captureSession.addOutput(stillImageOutput)
    }
    var videoConnection = stillImageOutput.connectionWithMediaType(AVMediaTypeVideo)

    if videoConnection != nil {

        // Show next step button
        self.view.bringSubviewToFront(self.nextStep)
        self.nextStep.hidden = false

        // Secure image
        stillImageOutput.captureStillImageAsynchronouslyFromConnection(videoConnection) {
            (imageDataSampleBuffer, error) -> Void in
                var imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(imageDataSampleBuffer)

                self.image = UIImage(data: imageData)

        }

        // Freeze camera preview
        captureSession.stopRunning()


    }

}

当用户点击按钮继续拍摄照片后调用此方法(它暂时将图像保存到相机胶卷,但当我让图像看起来不错时最终会做更多):

@IBAction func nextStepTapped(sender: UIButton) {

    // Save to camera roll & proceeed
    UIImageWriteToSavedPhotosAlbum(self.image, nil, nil, nil)

}

我猜问题出在 photoTaken() 函数中,但我不知道。这是我第一次在 Swift/Xcode 中编写应用程序,因此将不胜感激。同样,问题是保存的照片非常暗,我已经尝试了几乎所有我在互联网上可以找到的与此问题相关的方法,但没有任何效果。谢谢!

编辑:可能值得注意的是,我正在 iPhone 4S 上对此进行测试。会不会跟这个有关系?

"First time the app is executed, the first call to AVCaptureDeviceInput.deviceInputWithDevice() triggers a system dialog, asking the user to allow usage of the camera. This was introduced in some countries with iOS 7, and was extended to all regions with iOS 8. Until the user accepts the dialog, the camera input will send a stream of black frames."

您可以在这里找到更多信息:http://www.objc.io/issue-21/camera-capture-on-ios.html

问题是我在拍照时调用了这个:

 stillImageOutput.outputSettings = [AVVideoCodecKey: AVVideoCodecJPEG]
     if captureSession.canAddOutput(stillImageOutput) {
         captureSession.addOutput(stillImageOutput)
     }

我将该部分从 photoTaken()(拍摄照片时调用)移至 beginSession()(调用以启动相机),现在可以使用了。在按下照片按钮后设置输出可能会导致 lag/delay 并且无法使相机完全完成动作。