用相机拍摄的照片在iOS w / Swift上出来真的很黑 [英] Pictures taken with the camera come out really dark on iOS w/ Swift

查看:388
本文介绍了用相机拍摄的照片在iOS w / Swift上出来真的很黑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The camera preview looks perfect, but when I take a picture and save it to the Camera Roll the picture comes out extremely dark. Tried a bunch of threads on here but nothing solved it. Here's my code.

这会设置相机/预览和工作正常:

This sets up the camera/preview and works fine:

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

   }


b $ b

拍摄照片时调用这个方法:

This is called when the picture is taken:

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


    }

}

当用户点击按钮继续时,它被调用(它现在将图像保存到相机胶卷,但是当我获得图片看起来不错):

This is called after the picture is taken when the user hits the button to proceed (it saves the image to the camera roll for now, but will eventually do more when I get the images to look good):

@IBAction func nextStepTapped(sender: UIButton) {

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

}

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

My guess is that the issue is within the photoTaken() function, but I have no idea. This is my first time writing an app in Swift/Xcode, so any help would greatly be appreciated. Again, the issue is that the saved photo is extremely dark and I've tried pretty much everything I could find on the internet related to this problem, but nothing works. Thanks!

编辑:可能值得注意的是,我在iPhone 4S上测试这个。

Might be worth noting that I'm testing this on an iPhone 4S. Could that have anything to do with it?

推荐答案

问题是,我在呼叫这张照片时: / p>

The issue was that I was calling this as the photo was being taken:

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



我从photoTaken图片拍摄)到beginSession()(调用以启动相机),现在它的工作。在照片按钮按下后设置输出可能导致延迟/延迟,并且未使相机完全通过动作。

I moved that section from photoTaken() (called when a picture is taken) to beginSession() (called to initiate the camera) and now it works. Setting up the output after the photo button was pressed probably caused a lag/delay and didn't enable the camera to fully go through the motions.

这篇关于用相机拍摄的照片在iOS w / Swift上出来真的很黑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆