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

And*_*rew 8 camera ios avcapturesession swift

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

这将设置相机/预览并正常工作:

 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()

   }
Run Code Online (Sandbox Code Playgroud)

拍照时会调用此方法:

@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()


    }

}
Run Code Online (Sandbox Code Playgroud)

当用户按下按钮继续拍摄照片后调用此功能(此时它会将图像保存到相机胶卷,但最终会在图像看起来更好时执行更多操作):

@IBAction func nextStepTapped(sender: UIButton) {

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

}
Run Code Online (Sandbox Code Playgroud)

我的猜测是问题在photoTaken()函数中,但我不知道.这是我第一次在Swift/Xcode中编写应用程序,因此非常感谢任何帮助.再次,问题是保存的照片非常黑暗,我已经尝试了几乎所有我在互联网上找到的与此问题相关的内容,但没有任何作用.谢谢!

编辑:可能值得注意的是我在iPhone 4S上测试它.这与它有什么关系吗?

And*_*rew 18

问题是,我正在拍照,因为照片正在拍摄:

 stillImageOutput.outputSettings = [AVVideoCodecKey: AVVideoCodecJPEG]
     if captureSession.canAddOutput(stillImageOutput) {
         captureSession.addOutput(stillImageOutput)
     }
Run Code Online (Sandbox Code Playgroud)

我将该部分从photoTaken()(在拍摄照片时调用)移动到beginSession()(调用以启动相机),现在它可以正常工作.按下照片按钮后设置输出可能会导致延迟/延迟,并且无法使相机完全通过动作.