【问题标题】:Pictures taken with the camera come out really dark on iOS w/ Swift使用相机拍摄的照片在带有 Swift 的 iOS 上显得很暗
【发布时间】:2015-06-02 18:05:03
【问题描述】:

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

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

 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 上进行测试。会不会有什么关系?

【问题讨论】:

    标签: ios swift camera avcapturesession


    【解决方案1】:

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

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

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

    【讨论】:

    • 您知道是否有其他解决方案吗?想象一下,我正在构建一个相机应用程序,该应用程序在启动后立即拍照,而无需用户按下快门按钮。如何将输出添加到会话并尽快拍照而不会变暗?顺便说一句,我使用的是新的照片输出,而不是旧的静止图像输出。
    • 有谁知道当相机完全准备好时是否有回调/委托/通知?
    【解决方案2】:

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

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

    【讨论】:

    • 我接受了相机对话框。图片不是黑框,只是非常暗淡。预览看起来不错,然后当我点击拍照时,它会显着变暗并冻结黑暗图像上的预览。我会阅读该链接...
    • 该链接实际上是我一直在使用的教程。尝试了曝光/白平衡的东西,但没有帮助。
    猜你喜欢
    • 1970-01-01
    • 2016-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多