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



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

这设置了相机/预览并运行良好:

 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上测试这个。这和它有关系吗?

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

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

我把这个部分从photoTaken()(在拍摄照片时调用)移到了beginSession()(调用以启动相机),现在它可以工作了。在按下照片按钮后设置输出可能会导致延迟/延迟,并且无法使相机完全完成动作。

"第一次执行应用程序时,对AVCaptureDeviceInput的第一次调用。deviceInputWithDevice()会触发一个系统对话框,要求用户允许使用相机。这在一些iOS 7国家/地区引入,并在iOS 8中扩展到所有地区。在用户接受对话框之前,相机输入将发送一个黑帧流。"

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

最新更新