9

I have come up with an implementation of AVFoundation and ImageIO to take care of the photo taking in my application. I have an issue with it, however. The images I take are always dark, even if the flash goes off. Here's the code I use:

        [[self currentCaptureOutput] captureStillImageAsynchronouslyFromConnection:[[self currentCaptureOutput].connections lastObject]
                                        completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {

                                            [[[blockSelf currentPreviewLayer] session] stopRunning];
                                            if (!error) {
                                                NSData *data            = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
                                                CGImageSourceRef source = CGImageSourceCreateWithData((CFDataRef) data, NULL);

                                                if (source) {

                                                    UIImage *image = [blockSelf imageWithSource:source];
                                                    [blockSelf updateWithCapturedImage:image];
                                                    CFRelease(source);

                                                }

                                            }

                                        }];

Is there anything there that could cause the image taken to not include the flash?

1 Answer 1

15

I found I sometimes got dark images if the AVCaptureSession was set up immediately before this call. Perhaps it takes a while for the auto-exposure & white balance settings to adjust themselves.

The solution was to set up the AVCaptureSession, then wait until the AVCaptureDevice's adjustingExposure and adjustingWhiteBalance properties are both NO (observe these with KVO) before calling -[AVCaptureStillImageOutput captureStillImageAsynchronouslyFromConnection: completionHandler:].

2
  • 2
    How do you do that? If I observe both adjustingExposure and adjustingWhiteBalance - when exactly should I call captureStillImageAsynchronouslyFromConnection? Straight from observeValueForKeyPath?
    – grzaks
    Dec 1, 2014 at 22:10
  • Did you find how to do that? Because I'm having the exact same issue right now on my iPhone 6.
    – Sebastien
    Dec 4, 2014 at 7:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.